douya8978 2016-07-18 17:46
浏览 206
已采纳

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

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?

</div>
  • 写回答

1条回答 默认 最新

  • dpje52239 2016-07-18 17:55
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器