我想cURL google搜索结果在php
问题描述:
我尝试过下列代码:
$url = 'http://www.google.co.uk/#q='.$query.'&hl=en&prmd=imvns&source=lnt&tbs=ctr:countryUK%7CcountryGB&cr=countryUK%7CcountryGB&sa=X&psj=1&ei=m65DT_yUAcnG0QX46_yPDw&ved=0CEEQpwUoAQ&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=2e9b4f7fb1e75d0d&biw=1440&bih=799';
$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, '192.168.0.1:1501');
curl_setopt($ch, CURLOPT_REFERER, 'www.google.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
但它显示google首页而不是google搜索结果页。
帮助我解决这个问题。
But it shows google homepage instead of google search result page. Help me to resolve this problem.
答
我成功地绕过了google尝试防止curl
I was successfully able to bypass google's attempt to prevent curl search by the following:
$useragent = "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912/870; U; id) Presto/2.4.15";
$ch = curl_init ("");
curl_setopt ($ch, CURLOPT_URL, "http://www.google.com/search?hl=en&tbo=d&site=&source=hp&q=".$query);
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent); // set user agent
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
echo $output = curl_exec ($ch);
curl_close($ch);
请注意,我使用的用户代理是一个老歌剧迷你浏览器。
Note the user agent I used is an old opera mini browser. this way google displays an html content that you can parse.
这是针对Google服务条款的,请不要滥用;)
THIS IS AGAINST GOOGLE TOS, please do not abuse ;)
use $ query = urlencode($ query)