如何在 PHP 中使用 Google 搜索查询?
How to use Google Search query in PHP?
我正在尝试在我的网站中使用 Google 搜索查询,我需要获取我发送给查询的文本的网站 URL,该代码对于有限的结果工作正常,但在之后它停止工作有时,也许 Google 将其禁用一段时间?
代码如下:
$cleanQuery = str_replace(" ","+",$text);
$url = 'http://www.google.com/search?q='.$cleanQuery;
$scrape = file_get_contents($url);
$text是用户在搜索时输入的文本。但问题是它只能工作一段时间,然后就停止了。
工作示例:http://www.alleffort.com/tools/findurl.php
如果您在文本区域中输入一些文本,那么在提交时它应该会检索所有相关信息,但它不起作用。
问题可能是您附加到 url:
的字符串
$cleanQuery = str_replace(" ","+",$text);
这不能正确准备用于查询字符串的字符串,您需要编码的字符不只是 space。
相反,您应该使用 urlencode()
:
$cleanQuery = urlencode($text);
这段代码很可能会帮助您解决问题
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
我正在尝试在我的网站中使用 Google 搜索查询,我需要获取我发送给查询的文本的网站 URL,该代码对于有限的结果工作正常,但在之后它停止工作有时,也许 Google 将其禁用一段时间?
代码如下:
$cleanQuery = str_replace(" ","+",$text);
$url = 'http://www.google.com/search?q='.$cleanQuery;
$scrape = file_get_contents($url);
$text是用户在搜索时输入的文本。但问题是它只能工作一段时间,然后就停止了。
工作示例:http://www.alleffort.com/tools/findurl.php
如果您在文本区域中输入一些文本,那么在提交时它应该会检索所有相关信息,但它不起作用。
问题可能是您附加到 url:
的字符串 $cleanQuery = str_replace(" ","+",$text);
这不能正确准备用于查询字符串的字符串,您需要编码的字符不只是 space。
相反,您应该使用 urlencode()
:
$cleanQuery = urlencode($text);
这段代码很可能会帮助您解决问题
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>