[英]CURL works in command line but not in the PHP script
已經在這里提出了要求,但是詢問者沒有提供足夠的相關日志和代碼,因此我無法使用它來解決問題。 Cookie中沒有&符,因此肯定不是問題。
因此,我可以使用以下命令字符串在命令行中使用curl成功打開網頁:
curl "https://example.com" -H "cookie: wpfront-notification-bar-landingpage=1; gsScrollPos=; PHPSESSID=qhdkf5id0mnjcjadeuadsfkfp3; _gat=1; arp_scroll_position=877; _drip_client_1350063=vid^%^253D57a7eb508b0f013477ed126e9005972f^%^2526pageViews^%^253D8^%^2526sessionPageCount^%^253D2^%^2526lastVisitedAt^%^253D1478973050670^%^2526weeklySessionCount^%^253D2^%^2526lastSessionAt^%^253D1478972975034^%^2526form^%^255B2550^%^255D^%^255Bauto_open^%^255D^%^253D1478960007^%^2526form^%^255B2550^%^255D^%^255Bmanual_close^%^255D^%^253D1478960015; _ga=GA1.2.660807988.1478959677"
但是我的PHP腳本應該完全相同,但是不起作用。 我嘗試將Cookie添加到標頭中,並立即添加Cookie,但這是相同的。
function request($url) {
$ch = curl_init();
//curl_setopt($ch, CURLOPT_TIMEOUT_MS, 2000);
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_ENCODING => "gzip",
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_HEADER => TRUE,
CURLOPT_USERAGENT => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36",
CURLOPT_HTTPHEADER => array("cookie: wpfront-notification-bar-landingpage=1; gsScrollPos=; PHPSESSID=qhdkf5id0mnjcjadeuadsfkfp3; _gat=1; arp_scroll_position=877; _drip_client_1350063=vid^%^253D57a7eb508b0f013477ed126e9005972f^%^2526pageViews^%^253D8^%^2526sessionPageCount^%^253D2^%^2526lastVisitedAt^%^253D1478973050670^%^2526weeklySessionCount^%^253D2^%^2526lastSessionAt^%^253D1478972975034^%^2526form^%^255B2550^%^255D^%^255Bauto_open^%^255D^%^253D1478960007^%^2526form^%^255B2550^%^255D^%^255Bmanual_close^%^255D^%^253D1478960015; _ga=GA1.2.660807988.1478959677"),
CURLOPT_VERBOSE => 1,
));
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header = substr($response, 0, $header_size);
$html = substr($response, $header_size);
echo $html;
libxml_use_internal_errors(TRUE);
$dom = new DOMDocument();
if (strpos($url, 'feed') !== false) {
$dom->loadXML($html);
} else {
$dom->loadHtml($html);
}
return new DOMXPath($dom);
libxml_clear_errors(); // to clear memory up
}
request("http://example.com");
如何通過PHP腳本成功運行此curl? 為什么它在命令行中有效,但在PHP腳本中無效? 我有做錯什么嗎?
任何幫助,將不勝感激。
我通過使用shell_exec()
來解決它。
echo shell_exec("curl https://example.com");
似乎curl庫存在一個錯誤,加上該站點頁面中的一個錯誤。 該錯誤並不一致。 有時,命令行curl需要使用cookie,但大多數情況下不需要。
好吧,我沒有時間調試curl,所以如果shell_exec()
有效,我就可以了。 它甚至變得更加簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.