将CMD cURL转换为PHP

时间:2016-02-11 07:23:52

标签: php curl

我尝试卷曲this url这是curl cmd命令,在chrome中使用copy作为curl:

curl "http://b2b.sriwijayaair.co.id/SJ-Eticket/login.php?action=in" -H "Pragma: no-cache" -H "Accept-Encoding: gzip, deflate, sdch" -H "Accept-Language: en-US,en;q=0.8" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Cache-Control: no-cache" -H "Cookie: PHPSESSID=nupsnjthtmgedjcuguh80667v4; _ga=GA1.3.1626934475.1455171426" -H "Connection: keep-alive" --compressed

我在使用cmd时得到了我期望的结果,但是当我尝试转换为php时结果不一样

PHP代码:

$curl = curl_init();

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Pragma: no-cache',
        'Accept-Encoding: gzip, deflate, sdch',
        'Accept-Language: en-US,en;q=0.8',
        'Upgrade-Insecure-Requests: 1',
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Cache-Control: no-cache',
        'Cookie: PHPSESSID=nupsnjthtmgedjcuguh80667v4; _ga=GA1.3.1626934475.1455171426'
    ));
curl_setopt($curl, CURLOPT_URL, 'http://b2b.sriwijayaair.co.id/SJ-Eticket/login.php?action=in');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

$result = curl_exec($curl);

curl_close($curl);

print_r($result);

结果: enter image description here

我想念的是什么?

1 个答案:

答案 0 :(得分:0)

您不需要像Chrome那样发送所有标题。

此外,您在标题中缺少Cookie。试试这个标题。

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Cookie: PHPSESSID=nupsnjthtmgedjcuguh80667v4; _ga=GA1.3.1626934475.1455171426'
));

注意:您已从浏览器中复制此Cookie。所以它很快就会过期。