douhu8851
2014-12-02 04:11 阅读 171
已采纳

PHP curl返回空白页面

Can anyone show me how to compose this request using curl php? I need to scrape a website, but the code I use (given below) always returns a blank page. I want to send

Accept-Encoding, Useragent, and cookie 

Request:

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

Accept-Encoding: gzip

to

https://example.com/

Thanks!

The code I tried to use (returned a blank page):

<?
$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;
?>

Did I do something wrong?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanjie9630 duanjie9630 2014-12-02 05:00

    This worked for me :

    $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;
    
    点赞 评论 复制链接分享

相关推荐