PHP curl返回空白页面

时间:2014-12-02 04:11:53

标签: php curl

任何人都可以告诉我如何使用curl php撰写此请求吗?我需要抓一个网站,但我使用的代码(如下所示)总是返回一个空白页面。 我想发送

Accept-Encoding, Useragent, and cookie 

请求:

Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.99 Safari/535.1

Accept-Encoding: gzip

https://example.com/

谢谢!

我尝试使用的代码(返回空白页):

<?
$url='https://example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$request_headers = array();
$request_headers[] = 'Accept-Encoding: gzip';
$request_headers[] = 'Client: Apple';
curl_setopt($ch, CURLOPT_COOKIE, 'insertedmycookiehere');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.99 Safari/535.1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?>

我做错了吗?

1 个答案:

答案 0 :(得分:0)

这对我有用:

$url='http://example.com'; /* You incorrectly used https for this website */ 

$ch = curl_init($url);

$request_headers = array();
$request_headers[] = 'Accept-Encoding: gzip';
$request_headers[] = 'Client: Apple';

curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); /* Moved this line here */

curl_setopt($ch, CURLOPT_COOKIE, 'insertedmycookiehere');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.99 Safari/535.1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;