使用 PHP 从(搜索引擎)引用网址获取关键字
我正在尝试从引用网址获取搜索关键字。目前,我正在对 Google 网址使用以下代码。但有时它不起作用......
$query_get = "(q|p)";
$referrer = "http://www.google.com/search?hl=en&q=learn+php+2&client=firefox";
preg_match('/[?&]'.$query_get.'=(.*?)[&]/',$referrer,$search_keyword);
是否有另一种/干净/有效的方法来做到这一点?
谢谢你, 普拉萨德
I am trying to get the search keyword from a referrer url. Currently, I am using the following code for Google urls. But sometimes it is not working...
$query_get = "(q|p)";
$referrer = "http://www.google.com/search?hl=en&q=learn+php+2&client=firefox";
preg_match('/[?&]'.$query_get.'=(.*?)[&]/',$referrer,$search_keyword);
Is there another/clean/working way to do this?
Thank you,
Prasad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用 PHP5,请查看 http://php.net/parse_url 和 http://php.net/parse_str
示例:
If you're using PHP5 take a look at http://php.net/parse_url and http://php.net/parse_str
Example:
不同的搜索引擎有不同的查询字符串。在尝试了威廉的方法之后,我找到了自己的方法。 (因为,雅虎使用“p”,但有时使用“q”)
谢谢,
普拉萨德
There are different query strings on different search engines. After trying Wiliam's method, I have figured out my own method. (Because, Yahoo's is using 'p', but sometimes 'q')
Thank you,
Prasad
为了补充其他答案,请注意,包含搜索词的查询字符串参数因搜索提供程序而异。此 PHP 片段显示了要使用的正确参数:
来源:http:// /betterwp.net/wordpress-tips/get-search-keywords-from-referrer/
To supplement the other answers, note that the query string parameter that contains the search terms varies by search provider. This snippet of PHP shows the correct parameter to use:
Source: http://betterwp.net/wordpress-tips/get-search-keywords-from-referrer/
这应该适用于 Google、Bing,有时还适用于 Yahoo 搜索:
This one should work For Google, Bing and sometimes, Yahoo Search:
我相信谷歌和雅虎已经更新了他们的算法,以排除搜索关键字和网址中无法使用 http_referrer 方法接收的其他参数。
请告诉我上述建议是否仍会提供搜索关键字。
当我在我的网站端使用http引荐来源网址时,我现在收到的信息如下。
来自谷歌:https://www.google.co.in/
来自雅虎:https://in.yahoo.com/
参考:https://webmasters.googleblog.com/2012/03/upcoming-changes-in-谷歌-http.html
I believe google and yahoo had updated their algorithm to exclude search keywords and other params in the url which cannot be received using http_referrer method.
Please let me know if above recommendations will still provide the search keywords.
What I am receiving now are below when using http referrer at my website end.
from google: https://www.google.co.in/
from yahoo: https://in.yahoo.com/
Ref: https://webmasters.googleblog.com/2012/03/upcoming-changes-in-googles-http.html