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

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 前端 uniapp App端在离线状态如何使用modbus 连接手机蓝牙进行读写操控机器?
  • ¥15 SQL语句根据字段自动生成行
  • ¥500 “掌声响起来”软件(不确定性人工智能)
  • ¥500 我要找大模型一体机产品手册和应用案例
  • ¥60 用r语言进行数据分析
  • ¥20 关于游戏c++语言代码问题
  • ¥15 如何制作永久二维码,最好是微信也可以扫开的。(相关搜索:管理系统)
  • ¥15 delphi indy cookie 有效期
  • ¥15 labelme打不开怎么办
  • ¥35 按照图片上的两个任务要求,用keil5写出运行代码,并在proteus上仿真成功,🙏