使用 PHP 从(搜索引擎)引用网址获取关键字

发布于 2024-08-12 08:46:42 字数 326 浏览 4 评论 0原文

我正在尝试从引用网址获取搜索关键字。目前,我正在对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

〆一缕阳光ご 2024-08-19 08:46:42

如果您使用 PHP5,请查看 http://php.net/parse_urlhttp://php.net/parse_str

示例:

// The referrer
$referrer = 'http://www.google.com/search?hl=en&q=learn+php+2&client=firefox';

// Parse the URL into an array
$parsed = parse_url( $referrer, PHP_URL_QUERY );

// Parse the query string into an array
parse_str( $parsed, $query );

// Output the result
echo $query['q'];

If you're using PHP5 take a look at http://php.net/parse_url and http://php.net/parse_str

Example:

// The referrer
$referrer = 'http://www.google.com/search?hl=en&q=learn+php+2&client=firefox';

// Parse the URL into an array
$parsed = parse_url( $referrer, PHP_URL_QUERY );

// Parse the query string into an array
parse_str( $parsed, $query );

// Output the result
echo $query['q'];
情未る 2024-08-19 08:46:42

不同的搜索引擎有不同的查询字符串。在尝试了威廉的方法之后,我找到了自己的方法。 (因为,雅虎使用“p”,但有时使用“q”)

$referrer = "http://search.yahoo.com/search?p=www.stack+overflow%2Ccom&ei=utf-8&fr=slv8-msgr&xargs=0&pstart=1&b=61&xa=nSFc5KjbV2gQCZejYJqWdQ--,1259335755";
$referrer_query = parse_url($referrer);
$referrer_query = $referrer_query['query'];
$q = "[q|p]"; //Yahoo uses both query strings, I am using switch() for each search engine
preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword);
$keyword = urldecode($keyword[1]);
echo $keyword; //Outputs "www.stack overflow,com"

谢谢,
普拉萨德

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')

$referrer = "http://search.yahoo.com/search?p=www.stack+overflow%2Ccom&ei=utf-8&fr=slv8-msgr&xargs=0&pstart=1&b=61&xa=nSFc5KjbV2gQCZejYJqWdQ--,1259335755";
$referrer_query = parse_url($referrer);
$referrer_query = $referrer_query['query'];
$q = "[q|p]"; //Yahoo uses both query strings, I am using switch() for each search engine
preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword);
$keyword = urldecode($keyword[1]);
echo $keyword; //Outputs "www.stack overflow,com"

Thank you,
Prasad

天邊彩虹 2024-08-19 08:46:42

为了补充其他答案,请注意,包含搜索词的查询字符串参数因搜索提供程序而异。此 PHP 片段显示了要使用的正确参数:

$search_engines = array(
    'q' => 'alltheweb|aol|ask|ask|bing|google',
    'p' => 'yahoo',
    'wd' => 'baidu',
    'text' => 'yandex'
);

来源: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:

$search_engines = array(
    'q' => 'alltheweb|aol|ask|ask|bing|google',
    'p' => 'yahoo',
    'wd' => 'baidu',
    'text' => 'yandex'
);

Source: http://betterwp.net/wordpress-tips/get-search-keywords-from-referrer/

反话 2024-08-19 08:46:42
<?php 
class GET_HOST_KEYWORD 
{ 
    public function get_host_and_keyword($_url) { 
        $p = $q = "";
        $chunk_url = parse_url($_url); 
        $_data["host"] = ($chunk_url['host'])?$chunk_url['host']:''; 
        parse_str($chunk_url['query']); 
        $_data["keyword"] = ($p)?$p:(($q)?$q:''); 
        return $_data; 
    } 
}     
// Sample Example 
$obj = new GET_HOST_KEYWORD(); 
print_r($obj->get_host_and_keyword('http://www.google.co.in/search?sourceid=chrome&ie=UTF-&q=hire php php programmer')); 

// sample output
//Array
//(
//    [host] => www.google.co.in
//    [keyword] => hire php php programmer
//)

// $search_engines = array(
//    'q' => 'alltheweb|aol|ask|ask|bing|google',
//    'p' => 'yahoo',
//    'wd' => 'baidu',
//    'text' => 'yandex'
//);


?>
<?php 
class GET_HOST_KEYWORD 
{ 
    public function get_host_and_keyword($_url) { 
        $p = $q = "";
        $chunk_url = parse_url($_url); 
        $_data["host"] = ($chunk_url['host'])?$chunk_url['host']:''; 
        parse_str($chunk_url['query']); 
        $_data["keyword"] = ($p)?$p:(($q)?$q:''); 
        return $_data; 
    } 
}     
// Sample Example 
$obj = new GET_HOST_KEYWORD(); 
print_r($obj->get_host_and_keyword('http://www.google.co.in/search?sourceid=chrome&ie=UTF-&q=hire php php programmer')); 

// sample output
//Array
//(
//    [host] => www.google.co.in
//    [keyword] => hire php php programmer
//)

// $search_engines = array(
//    'q' => 'alltheweb|aol|ask|ask|bing|google',
//    'p' => 'yahoo',
//    'wd' => 'baidu',
//    'text' => 'yandex'
//);


?>
乱世争霸 2024-08-19 08:46:42
$query = parse_url($request, PHP_URL_QUERY);
$query = parse_url($request, PHP_URL_QUERY);
神经大条 2024-08-19 08:46:42

这应该适用于 Google、Bing,有时还适用于 Yahoo 搜索:

if( isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']) {
    $query = getSeQuery($_SERVER['HTTP_REFERER']);
    echo $query;
} else {
    echo "I think they spelled REFERER wrong? Anyways, your browser says you don't have one.";
}



function getSeQuery($url = false) {
    $segments = parse_url($url);
    $keywords = null;
    if($query = isset($segments['query']) ? $segments['query'] : (isset($segments['fragment']) ? $segments['fragment'] : null)) {
    parse_str($query, $segments);
    $keywords = isset($segments['q']) ? $segments['q'] : (isset($segments['p']) ? $segments['p'] : null);
    }
    return $keywords;
}

This one should work For Google, Bing and sometimes, Yahoo Search:

if( isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']) {
    $query = getSeQuery($_SERVER['HTTP_REFERER']);
    echo $query;
} else {
    echo "I think they spelled REFERER wrong? Anyways, your browser says you don't have one.";
}



function getSeQuery($url = false) {
    $segments = parse_url($url);
    $keywords = null;
    if($query = isset($segments['query']) ? $segments['query'] : (isset($segments['fragment']) ? $segments['fragment'] : null)) {
    parse_str($query, $segments);
    $keywords = isset($segments['q']) ? $segments['q'] : (isset($segments['p']) ? $segments['p'] : null);
    }
    return $keywords;
}
远山浅 2024-08-19 08:46:42

我相信谷歌和雅虎已经更新了他们的算法,以排除搜索关键字和网址中无法使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文