实现google查询 需要外国服务器支持。

唉………苦逼

CURL版本

<base href="http://www.google.com.hk/">
<meta charset='utf-8'>
<?php
/**
 *
 * @author caihaibin
 */
@header ( "Content-Type: text/html;charset=utf-8" );
if (@$_REQUEST ['q']) {
	$data = urlencode ( $_REQUEST ['q'] );
	$url = "http://www.google.com.hk/search?hl=zh-CN&source=hp&q={$data}&btnG=Google+%E6%90%9C%E7%B4%A2&meta=lr%3Dlang_zh-CN&aq=f&aqi=&aql=&oq=&gs_rfai=";
} else {
	$url = "http://www.google.com.hk/";
}
$cookie_file = dirname ( __FILE__ ) . "/temp/google.txt";
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_USERAGENT, $_SERVER ['HTTP_USER_AGENT'] );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookie_file );
$contents = curl_exec ( $ch );
curl_close ( $ch );
$contents = preg_replace ( '/<form action="/search" id="tsf" method="GET"/', '<form action="http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['SCRIPT_NAME'] . '?caihaibin=ok" id="tsf" method="POST"', $contents );
echo $contents;
?>

HttpClient.class.php版本

<base href="http://www.google.com.hk/">
<?php
require 'HttpClient.class.php';
header ( "Content-Type: text/html;charset=utf-8" );
function test_cb($res, $req, $key) {
	// echo $res->body;
	// echo '[' . $key . '] url: ' . $req->getUrl() . ', ';
	// echo 'time cost: ' . $res->timeCost . ', ';
	// echo 'size: ' . number_format(strlen($res->body)) . "<Br>";
}

$http = new HttpClient ( 'test_cb' );
// 全部 URL 抓取完毕时一并返回,传入单个 URL 或数组组成的多个 URL
// 第一次请求可能因为域名解析等原因较慢
// 可以自行构造 HttpRequest 直接用 IP请求更快
if (@$_REQUEST ['q']) {
	$data = urlencode ( $_REQUEST ['q'] );
	$url = "http://www.google.com.hk/search?hl=zh-CN&source=hp&q={$data}&btnG=Google+%E6%90%9C%E7%B4%A2&meta=lr%3Dlang_zh-CN&aq=f&aqi=&aql=&oq=&gs_rfai=";
} else {
	$url = "http://www.google.com.hk/";
}
$results = $http->get ( array (
		'google' => $url 
) );
$contents = $results ['google']->body;
$contents = preg_replace ( '/<form action="/search" /', '<form action="http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['SCRIPT_NAME'] . '" method="POST"', $contents );
echo $contents;

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注