Curl命令有效,但用PHP编写它不起作用。为什么?

时间:2016-07-18 17:46:23

标签: php curl

我在VPS上运行Linux。

当我运行此命令时:

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

...我得到了网站的源代码。

我尝试在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);

但是,此代码的结果是:NULL

我想知道我的代码有什么问题?

1 个答案:

答案 0 :(得分:3)

您可以使用我的源代码

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);

我的结果

enter image description here