douxing6532 2012-12-13 13:31
浏览 87

PHP CURL - 在抓取时存储和使用cookie的问题

I've been trying to write a script that retrieves Google trends results for a given keyword. Please note im not trying to do anything malicious I just want to be able to automate this process and run it a few times every day.

After investigating the Google trends page I discovered that the information is available using the following URL:

http://www.google.com/trends/trendsReport?hl=en-GB&q=keyword&cmpt=q&content=1

You can request that information mutliple times with no issues from a browser, but if you try with "privacy mode" after 4 or 5 requests the following is displayed:

An error has been detected You have reached your quota limit. Please try again later.

This makes me think that cookies are required. So I have written my script as follows:

$cookiefile = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/cookies.txt';


$url = 'http://www.google.com/trends/trendsReport?hl=en-GB&q=keyword&cmpt=q&content=1'; 
$ch = curl_init();      

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);        
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);


$x='error';
while (trim($x) != ''  ){
     $html=curl_exec($ch);
     $x=curl_error($ch);
}

echo  "test cookiefile contents = ".file_get_contents($cookiefile)."<br />";
echo $html;

However I just can't get anything written to my cookies file. So I keep on getting the error message. Can anyone see where I'm going wrong with this?

  • 写回答

1条回答 默认 最新

  • doubengman2072 2012-12-13 13:36
    关注

    I'm pretty sure your cookie file should exist before you can use it with curl. Try:

    $h = fopen($cookiefile, "x+");
    
    评论

报告相同问题?