lundi 26 juin 2017

Curl doesn't get 302 page

Hello Im trying to make some automation in social network. I logged in with curl with success, but Im stuck on another actions.

The thing is Im sending POST request with cookies after login and parameters parsed from html form to form action url and Curl returns http code 200 on this request but it should return 302 (I tried FOLLOWLOCATION false).

I retrieved all data from curl request and tried to send it via POSTMAN app for linux. And it works. I don't understand why it doesn't work with curl.

public function requestPost($url, array $postData)
{
    $this->setRequest($url);
    curl_setopt($this->curlResource, CURLOPT_POSTFIELDS, http_build_query($postData));
    return curl_exec($this->curlResource);
}

/**
 * Common function for Post/Get setup.
 *
 * @param string $url
 *   Url to send request to.
 */
private function setRequest($url)
{
    curl_setopt($this->curlResource, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($this->curlResource, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->curlResource, CURLOPT_POST, true);
    curl_setopt($this->curlResource, CURLOPT_USERAGENT, self::USER_AGENT);
    curl_setopt($this->curlResource, CURLOPT_URL, $url);
    curl_setopt($this->curlResource, CURLINFO_HEADER_OUT, 1);
    curl_setopt($this->curlResource, CURLOPT_VERBOSE, 1);

    // Save cookies in memory until curl_close  is not called. Windows use NULL.
 //        curl_setopt($this->curlResource, CURLOPT_COOKIEJAR, '\\' === DIRECTORY_SEPARATOR ? NULL : '/dev/null');

    $tmpfname = dirname(__FILE__).'/cookie.txt';
    curl_setopt($this->curlResource, CURLOPT_COOKIEJAR,  $tmpfname);
    curl_setopt($this->curlResource, CURLOPT_COOKIEFILE, $tmpfname);

    // Set proxy settings.
    if ($this->proxy)
    {
        list($proxyType, $proxyIp, $proxyPort, $proxyLogin, $proxyPass) = explode(":", $this->proxy);
        curl_setopt($this->curlResource, CURLOPT_PROXY, $proxyIp);
        curl_setopt($this->curlResource, CURLOPT_PROXYPORT, $proxyPort);
        curl_setopt($this->curlResource, CURLOPT_PROXYTYPE, $proxyType == 'http' ? CURLPROXY_HTTP : CURLPROXY_SOCKS5);
        curl_setopt($this->curlResource, CURLOPT_PROXYUSERNAME, $proxyLogin);
        curl_setopt($this->curlResource, CURLOPT_PROXYPASSWORD, $proxyPass);
        curl_setopt($this->curlResource, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->curlResource, CURLOPT_SSL_VERIFYHOST, false);
    }
}

Same code has been used for login operation.




Aucun commentaire:

Enregistrer un commentaire