如何在PHP中使用Google搜索查询? [英] How to use Google Search query in PHP?
问题描述
我正在尝试在我的网站中使用Google搜索查询,我需要获取发送给该查询的文本的网站网址,该代码可以很好地处理有限的结果,但是一段时间后它将停止工作,也许Google已禁用它有一段时间了?
I am trying to use Google Search query in my website, I need to get website URLs for the text I sent to the query, the code works fine for limited results, but then it stops working after some time, maybe Google disables it for some time?
这是代码:
$cleanQuery = str_replace(" ","+",$text);
$url = 'http://www.google.com/search?q='.$cleanQuery;
$scrape = file_get_contents($url);
$ text是用户在搜索时输入的文本.但是问题是它只能在一段时间内起作用,然后停止.
$text is the text entered by the user while searching. But the problem is it works only for sometime, then it stops.
工作示例: http://www.alleffort.com/tools/findurl.php
如果您在文本区域中输入了一些文本,则在提交时应该检索所有相关信息,但是它不起作用.
If you enter some text in the textarea, then on submits it should retrieve all the related information, but it is not working.
推荐答案
问题可能出在您要附加到url的字符串上:
The problem is probably the string you are appending to the url:
$cleanQuery = str_replace(" ","+",$text);
这不能正确地准备要在查询字符串中使用的字符串,您将需要编码更多的字符,而不仅仅是空格.
This does not prepare the string correctly for use in a query string, you would need to encode more characters than just the space.
相反,您应该使用 urlencode()
:
Instead, you should use urlencode()
:
$cleanQuery = urlencode($text);
这篇关于如何在PHP中使用Google搜索查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!