Ottieni parole chiave da un URL di riferimento (motore di ricerca) utilizzando PHP
-
05-07-2019 - |
Domanda
Sto cercando di ottenere la parola chiave di ricerca da un URL referrer. Attualmente sto usando il seguente codice per gli URL di Google. Ma a volte non funziona ...
$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);
Esiste un altro modo / pulito / funzionante per farlo?
Grazie, Prasad
Soluzione
Se stai usando PHP5 dai un'occhiata a http://php.net/parse_url e http://php.net/parse_str
Esempio:
// 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'];
Altri suggerimenti
Esistono stringhe di query diverse su motori di ricerca diversi. Dopo aver provato il metodo di Wiliam, ho capito il mio metodo. (Perché, Yahoo sta usando 'p', ma a volte '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"
Grazie, Prasad
Per integrare le altre risposte, si noti che il parametro della stringa di query che contiene i termini di ricerca varia in base al provider di ricerca. Questo frammento di PHP mostra il parametro corretto da usare:
$search_engines = array(
'q' => 'alltheweb|aol|ask|ask|bing|google',
'p' => 'yahoo',
'wd' => 'baidu',
'text' => 'yandex'
);
Fonte: http://betterwp.net/wordpress-tips / get-ricerca-parole-da-referrer /
<?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'
//);
?>
$query = parse_url($request, PHP_URL_QUERY);
Questo dovrebbe funzionare per Google, Bing e talvolta Yahoo Search:
if( isset( Questo dovrebbe funzionare per Google, Bing e talvolta Yahoo Search:
<*>SERVER['HTTP_REFERER']) && Questo dovrebbe funzionare per Google, Bing e talvolta Yahoo Search:
<*>SERVER['HTTP_REFERER']) {
$query = getSeQuery( Questo dovrebbe funzionare per Google, Bing e talvolta Yahoo Search:
<*>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;
}
Credo che Google e Yahoo abbiano aggiornato il loro algoritmo per escludere parole chiave di ricerca e altri parametri nell'URL che non possono essere ricevuti utilizzando il metodo http_referrer.
Per favore fatemi sapere se i consigli di cui sopra forniranno comunque le parole chiave di ricerca.
Quello che sto ricevendo ora sono di seguito quando utilizzo il referrer http alla fine del mio sito Web.
da google: https://www.google.co.in/ da yahoo: https://in.yahoo.com/
Rif: https: //webmasters.googleblog .com / 2012/03 / imminenti-cambiamenti-in-googles-http.html