client = $client; $this->config = $config; } /** * @return Client */ public function getClient() { return $this->client; } /** * Check if it's connected * (if there's a "logout" link) * * @return bool */ public function isConnected() { if (!$this->is_connected) { $crawler = $this->client->request('GET', self::HOME_URL); $this->is_connected = $crawler->filter('.loggedOut')->count() == 0; } return $this->is_connected; } /** * Check if connected, if not, try to connect you * * @see this#isConnected * * @return bool */ public function connect() { if (!$this->isConnected()) { $crawler = $this->client->request('GET', self::LOGIN_URL); $form = $crawler->selectButton('Se connecter')->form(); $crawler = $this->client->submit($form, ['st_username' => $this->config->login, 'st_passwd' => $this->config->password]); $this->is_connected = $crawler->filter('.account_userinfo')->count() > 0; } return $this->is_connected; } /** * Retrieve all deals from the current account * * @return Deals */ public function getDeals() { if ($this->connect()) { $crawler = $this->client->request('GET', self::ACCOUNT_URL); $deals = $crawler->filter('#dashboard .list .element')->each( function ($node) { return $node->filter('.detail .title')->each( function ($n) { return $n->filter('a')->first()->attr('href'); } ); } ); return new Deals($this, $deals); } return new Deals($this, []); } public function addDeal(Deal $deal) { try { $t = date('YmdHis'); $log_folder = sprintf('%s/logs/%s', APP_DIR, $t); mkdir($log_folder); $crawler = $this->client->request('GET', self::ADD_URL); file_put_contents(sprintf('%s/add.html', $log_folder), $crawler->html()); $form = $crawler->selectButton('Valider')->form(); $image0 = fopen(sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId()), 'r'); $fields = [ 'check_type_diff' => 0, 'company_ad' => 0, // TODO what are those ? 'location_p' => sprintf('%s %s', $this->config->city, $this->config->postal_code), 'zipcode' => $this->config->postal_code, 'city' => $this->config->city, 'region' => $this->config->region, 'dpt_code' => $this->config->department, 'address' => $this->config->address, 'name' => $this->config->name, 'email' => $this->config->login, 'phone' => $this->config->phone, 'category' => $deal->getCategory(), 'type' => $deal->getType(), 'subject' => $deal->getSubject(), 'body' => $deal->getBody(), 'price' => $deal->getPrice(), 'image0' => $image0, 'no_salesmen' => 1, 'phone_hidden' => 0, ]; array_walk($fields, function (&$item, $key) { $item = [ 'name' => $key, 'contents' => $item, ]; }); var_dump($form->getUri()); // $crawler = $this->client->request('POST', $form->getUri(), ['Content-Type => multipart/form-data'], [], [], $fields); // This one doesn't wor either -> redirect to Home $crawler = $this->client->request('POST', $form->getUri(), ['multipart' => $fields]); var_dump($crawler->html()); var_dump($crawler); file_put_contents(sprintf('%s/add-1.html', $log_folder), $crawler->html()); $form = $crawler->selectButton('Valider mon annonce')->form(); // TODO accept_rule checked and valid form } catch (\Exception $e) { echo $e->getTraceAsString(); } } }