Get Original Referrer URL from Google Search with PHP? - php

I am using $_SERVER['HTTP_REFERER'];, to getting Referrer URL.
When I typed in Google search box the q='some text'
`https://www.google.com.pk/#hl=en&output=search&sclient=psy-ab&q=some text%2Ftestbulkresponse&oq=some text%2Ftestbulkresponse&gs_l=hp.3...15460.24280.1.25007.30.30.0.0.0.0.325.7136.2-27j3.30.0...0.0...1c.1.8.hp.dAvuch3bBg4&psj=1&bav=on.2,or.r_qf.&bvm=bv.44697112,d.ZWU&fp=980e418276b62e8c&biw=1366&bih=595`
but when I get this URL on my website using as $_SERVER['HTTP_REFERER'];
the q=null like this
`http://www.google.com.pk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CC0QFjAA&url=http%3A%2F%2Fwww.bulkresponse.com%2Ftestbulkresponse%2Fdashboard.php&ei=r9NbUfv7GcjaOYDdgKgC&usg=AFQjCNF9U_DpJEwupZ0ZLPbjWJ6DQLWZcA&bvm=bv.44697112,d.ZWU`.
So I am unable to get searched keywords in Google, what should I can do to get searched keywords?

Google removes the search query for HTTPS connections and logged users so the only way to know the search query is using Google Analytics
When you search from https://www.google.com, websites you visit from our organic search listings will still know that you came from Google, but won't receive information about each individual query.
http://googleblog.blogspot.com.es/2011/10/making-search-more-secure.html
http://analytics.blogspot.com.es/2011/10/making-search-more-secure-accessing.html

Try this.
parse_str(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $queries);
echo $queries['q'];
References:
http://php.net/parse_url
http://php.net/parse_str

$ref = $_SERVER['HTTP_REFERER'];
if(strstr($ref, "google.com")){
//echo $ref;
$regex ='/q=(.+?)&/';
preg_match($regex, $ref, $query);
echo $query[1];
}
it doesn't work everytime, especially now with their updates. I get " &esrc=s " reported a lot.

Related

how to fetch a page's likes from fb by php

i want to make a social media audit tool and i want to get likes of a person's Fb page. Basically what i want is i want that person to enter his FB pages URL and then i want to fetch the likes of his page and echo it in my PHP page. Is there any way I can do that without using graph API or any API. I just want a simple piece of code.
I have searched many questions regarding my project on web and on StackOverflow as well but coudn't find what i wanted, at last, I am asking this question. Is there anyone who can help me regarding this?
Thanx in advance.
<?php
$file = "https://www.facebook.com/IntellectualIndies/?epa=SEARCH_BOX";
$data = file_get_contents($file);
preg_match_all ('~<div class=\'_4bl9\'>\s*(<div.*?</div>\s*)?(.*?)</div>~is', $data, $matches);
$content = $matches[1];
$total = count($content);
for($i=0; $i<$total; $i++){
echo $content[$i]."<br />";
}
?>
i tried this code.
Is there any way I can do that without using graph API or any API
No, scraping is not allowed on Facebook.
There is only one way to do this:
Apply for Page Public Content Access
Use the Graph API with the following endpoint/field: /page-id?fields=fan_count
API Reference with example code: https://developers.facebook.com/docs/graph-api/reference/page/

trouble with google map

I have any adress recorded in my databse corresponding to the customers.
The trouble I met is concerning the including of a googlemap.
I have the following code that convert an adress to the polares.
function getCoordonnees($adresse) {
$url = "http://maps.google.com/maps/geo?q=" . urlencode($adresse) .
"&output=csv";
$csv = file($url);
$donnees = preg_split("#,#", $csv[0]);
return $donnees[2] . "," . $donnees[3];
}
The following code returns to me somethinbg like that
48.9381235,2.1329083
When I use the google map I have a picture like that :
Which comes from this url (generated by my function
http://maps.google.com/staticmap?center=48.9381235,2.1329083&zoom=15&size=300x300&maptype=mobile&markers=48.9381235,2.1329083,red&key=***********************KEY*GOOGLEMAP************&sensor=false
When I try to display that picture in my website, An error appears,
<img src="http://maps.google.com/staticmap?center=<?php echo $polar; ?>&zoom=15&size=300x300&maptype=mobile&markers=<?php echo $polar; ?>,red&key=***********************KEY*GOOGLEMAP************&sensor=false" style="border:double 2px" />
Where $polar is the polares.
Including this code in my website returns to me the following mistake :
Failed to load resource: the server responded with a status of 403 (Forbidden)
I have no idea from where comes this error. Actualy I do not think it comes from google map because on stackoverflow, the picture is displayed.
So I would ask if anybody know where can comes this mistake ? Is is from the server configuration or something else ? I've been looking for that since many times without answer.
Anykind of help will be much appreciated.
I was having the same issue and I decided to turn on Google Maps API v2
I already had Google Maps API v3 on but before messing with to many settings I turned them both on.
Now it works

How to get the referer search query from google?

As recently as two days ago, the following code worked to get the search query from google:
$refer = parse_url($_SERVER['HTTP_REFERER']);
$host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
$query = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);
if(strstr($host,'www.google.com'))
{
//do google stuff
$qstart = strpos($query, 'q=') +2;
$qend = strpos($query, '&', $qstart);
$qlength = $qend - $qstart;
$querystring = substr($query, $qstart, $qlength);
$querystring = str_replace('q=','',$querystring);
$keywords = explode('%20',$querystring);
$keywords = implode(' ', $keywords);
return $keywords;
}
However, now it does not. I tested it by using echo($query) and it appears that the way google processes referrer query requests has changed. Previously $query included
"q=term1%20term2%20term3%20...
Now, however, I am getting the following output when $query is echo'ed:
sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CCsQFjAB&url=http%3A%2F%2Fexample.com%2F&ei=vDA-UNnxHuOjyAHlloGYCA&usg=AFQjCNEvzNXHULR0OvoPMPSWxIlB9-fmpg&sig2=iPinsBaCFuhCLGFf0JHAsQ
Is there a way to get around this?
Sorry to say, but it's global Google politics change.
See web link
http://googlewebmastercentral.blogspot.ru/2012/03/upcoming-changes-in-googles-http.html
This means if user sign in Google account.
You can try it yourself: if your Google search url starts with https:// this means Google will hide some scratch parameters for the sake of privacy.
I too ran into the same issue this week. I'm not sure if this is still relevant to you, but what I found was that Google initiated SSL (Secure Sockets Layer) search for users who were signed in about a year ago, and it looks like SSL search may now be applied to all Google search queries. When I tested this, I was not signed in to Google and was using Firefox and still got the encrypted referring query.
This article has some helpful background and some ideas for working without specific search term data: http://searchenginewatch.com/article/2227114/5-Tips-for-Handling-Not-Provided-Data
// take the referer
$thereferer = strtolower($_SERVER['HTTP_REFERER']);
// see if it comes from google
if (strpos($thereferer,"google")) {
// delete all before q=
$a = substr($thereferer, strpos($thereferer,"q="));
// delete q=
$a = substr($a,2);
// delete all FROM the next & onwards
if (strpos($a,"&")) {
$a = substr($a, 0,strpos($a,"&"));
}
// we have the results.
$mygooglekeyword = urldecode($a);
}
Google initiated SSL for all searches and the information is only available via Google Analytics.
However, for paid campaigns search engines like Google, Bing and Yahoo use query string parameters such as utm_parameters and you can access the search query from the parameter utm_term.

Facebook Graph API Search without logged in user

I'm trying to issue a Facebook Graphp API search call for groups with a specific search term. The important fact is to search for groups not only beginning with the term but also to search for the term within the group name. So something like that.
FQL => SELECT * FROM groups WHERE groupname LIKE '%term%'
As far as i know this isn't possible in FQL. Therefore I have to use the Graph API search.
http://developers.facebook.com/docs/api#search
But I have to issue th call even if the user isn't logged in. Is this possible
or is there a possibility to log in a default user with some curl calls without user interaction (without displaying a form)?
If there is a simplier solution (for instance with FQL) please tell me.
I work with the graph API and not with the FQL so I'm not 100% sure on the differences etc etc... however a way I'd try is using the cURL script at http://developers.facebook.com/docs/authentication/#authenticating-users-in-a-web-application to get an auth token, then you must add the received auth token to the graph parameters as you can see on the site (a good way to test in the graph api is to click on the example likes or queries given while logged into facebook then copying the api code and breaking up the process like that [testing that you can get results you want from the query, testing that you can get your auth token then combining] so that you know what parts are 'problem' parts).
If that fails, scroll down to the "single sign-on with java-script" and have a look at the code they use to get the auth token from the facebook cookie, I notced you may be able to access that cookie from FQL.
I hope this was of some help! Please tell me if those ideas didn't work.
Jon
Using PHP-SDK 3.1.1, no auth or access_token needed. This sample assumes you have PHP-SDK installed on the page.
Use a form post the question to the page, arguments for get and post
are included. This will return the array from search https://graph.facebook.com/search?q=facebook&type=group
<?php
$q = urlencode($_GET['qs']);
if(!$_GET['qs']){
$q = urlencode($_POST['qs']);
if(!$_POST['qs']){
$q = "facebook";
}
}
$MEsearch = $facebook->api('/search?q='.$q.'&type=group');
foreach ($MEsearch as $key=>$value) {
$i=1;
foreach ($value as $fkey=>$fvalue) {
$i++;
if($fvalue[id]=="h"){
}else{
$groupname = $fvalue[name];
$groupid = $fvalue[id];
$groupversion = $fvalue[version];
echo $groupname. '<br />';
echo $groupid. '<br />';
echo $groupversion. '<br /><hr />';
}
};
};
?>
Sample Usage http://shawnsspace.com/plugins/photofeed.php Click Get Plugin at the bottom and use the search box to see this sample in action.

Is it possible to capture search term from Google search?

This may be a stupid question, but is it possible to capture what a user typed into a Google search box, so that this can then be used to generate a dynamic page on the landing page on my Web site?
For example, let's say someone searches Google for "hot dog", and my site comes up as one of the search result links. If the user clicks the link that directs them to my Web site, is it possible for me to somehow know or capture the "hot dog" text from the Google search box, so that I can call a script that searches my local database for content related to hot dogs, and then display that? It seems totally impossible to me, but I don't really know. Thanks.
I'd do it like this
$referringPage = parse_url( $_SERVER['HTTP_REFERER'] );
if ( stristr( $referringPage['host'], 'google.' ) )
{
parse_str( $referringPage['query'], $queryVars );
echo $queryVars['q']; // This is the search term used
}
This is an old question and the answer has changed since the original question was asked and answered. As of October 2011 Google is encrypting this referral information for anyone who is logged into a Google account: http://googleblog.blogspot.com/2011/10/making-search-more-secure.html
For users not logged into Google, the search keywords are still found in the referral URL and the answers above still apply. However, for authenticated Google users, there is no way to for a website to see their search keywords.
However, by creating dedicated landing pages it might still be possible to make an intelligent guess. (Visitors to the "Dignified charcoal sketches of Jabba the Hutt" page are probably...well, insane.)
Yes, it is possible. See HTTP header Referer. The Referer header will contain URL of Google search result page.
When user clicks a link on a Google search result page, the browser will make a request to your site with this kind of HTTP header:
Referer: http://www.google.fi/search?hl=en&q=http+header+referer&btnG=Google-search&meta=&aq=f&oq=
Just parse URL from request header, the search term used by user will be in q -parameter. Search term used in above example is "http header referer".
Same kind of approach usually works also for other search engines, they just have different kind of URL in Referer header.
This answer shows how to implement this in PHP.
Referer header is only available with HTTP 1.1, but that covers just about any somewhat modern browser. Browser may also forge Referer header or the header might be missing altogether, so do not make too serious desicions based on Referer header.
This is an old question but I found out that google no more gives out the query term because it by default redirects every user to https which will not give you the "q"parameter. Unless someone manually enters the google url with http (http://google.com) and then searches, there is no way as of now to get the "q" parameter.
Yes, it comes in the url:
http://www.google.com/search?hl=es&q=hot+dog&lr=&aq=f&oq=
here is an example:
Google sends many visitors to your site, if you want to get the keywords
they used to come to your site, maybe to impress them by displaying it
back on the page, or just to store the keyword in a database, here's the
PHP code I use :
// take the referer
$thereferer = strtolower($_SERVER['HTTP_REFERER']);
// see if it comes from google
if (strpos($thereferer,"google")) {
// delete all before q=
$a = substr($thereferer, strpos($thereferer,"q="));
// delete q=
$a = substr($a,2);
// delete all FROM the next & onwards
if (strpos($a,"&")) {
$a = substr($a, 0,strpos($a,"&"));
}
// we have the results.
$mygooglekeyword = urldecode($a);
}
and we can use <?= $mygooglekeywords ?> when we want to output the
keywords.
You can grab the referring URL and grab the search term from the query string. The search will be in the query as "q=searchTerm" where searchTerm is the text you want.
Same thing, but with some error handling
<?php
if (#$_SERVER['HTTP_REFERER']) {
$referringPage = parse_url($_SERVER['HTTP_REFERER']);
if (stristr($referringPage['host'], 'google.')) {
parse_str( $referringPage['query'], $queryVars );
$google = $queryVars['q'];
$google = str_replace("+"," ",$google); }
else { $google = false; }}
else { $google = false; }
if ($google) { echo "You searched for ".$google." at Google then came here!"; }
else { echo "You didn't come here from Google"; }
?>
Sorry, a little more
Adds support for Bing, Yahoo and Altavista
<?php
if (#$_SERVER['HTTP_REFERER']) {
$referringPage = parse_url($_SERVER['HTTP_REFERER']);
if (stristr($referringPage['host'], 'google.')
|| stristr($referringPage['host'], 'bing.')
|| stristr($referringPage['host'], 'yahoo.')) {
parse_str( $referringPage['query'], $queryVars );
if (stristr($referringPage['host'], 'google.')
|| stristr($referringPage['host'], 'bing.')) { $search = $queryVars['q']; }
else if (stristr($referringPage['host'], 'yahoo.')) { $search = $queryVars['p']; }
else { $search = false; }
if ($search) { $search = str_replace("+"," ",$search); }}
else { $search = false; }}
else { $search = false; }
if ($search) { echo "You're in the right place for ".$search; }
?>

Categories