Edward Trọng
Edward Trọng

Reputation: 61

Curl command works, but written in PHP it does not work. Why?

I am running Linux on a VPS.

When I run the this command:

curl https://www.bloomingdales.com/account/signin -H "Cookie: ewqeqweq" -X GET

... I get the source code of the site.

I tried to write the same command in PHP:

$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.bloomingdales.com/account/signin');
    curl_setopt($ch, CURLOPT_ENCODING ,"");
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
    curl_setopt($ch, CURLOPT_HEADER, array("Cookie: ewqeqweq"));
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_REFERER, 'https://www.bloomingdales.com/account/signin'); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0');
    $get_ = curl_exec($ch);
    echo $get_;
    curl_close($ch);

However, the result of this code is: NULL.

I am wondering what is wrong in my code?

Upvotes: 1

Views: 92

Answers (1)

Quynh Nguyen
Quynh Nguyen

Reputation: 3009

You can use my source code

function _curl($url,$post="",$usecookie = false,$_sock = false,$timeout = false) {  
    $ch = curl_init();
    if($post) {
        curl_setopt($ch, CURLOPT_POST ,1);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
    }
    if($timeout){
        curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    }
    if($_sock){
            curl_setopt($ch, CURLOPT_PROXY, $_sock);
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
    }
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10"); 
    if ($usecookie) { 
        curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie); 
        curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);    
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language: en-US,en;q=0.5',
        'Accept-Encoding: zip, deflate, sdch'
    ));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    $result=curl_exec ($ch); 
    curl_close ($ch);
    return $result;
}
$socks5 = '176.126.196.52:24369';
$cookie = tempnam('cookies','coo'.rand(1000000,9999999)); 
$url = "https://www.bloomingdales.com/account/signin";
$post = "";
$s = _curl($url,$post,$cookie,$socks5,'');
echo $s;
unlink($cookie);

My result

enter image description here

Upvotes: 3

Related Questions