how can get google search result - php

How can I get the result of search in Google search, but without using the API.
I've tried many ways, but Google detects the ip server and prints an error.
The function I use:
fopen
fsocket
...
For example this function can get html from other web sites, but for Google search, Google returns an error.
Is there a way I can get search results from Google?
function openpage($ur)
{
$buf="";
$ur=str_replace(" ","+",$ur);
$fp = fopen($ur, "rb");
if ($fp)
{
while(!feof($fp)) $buf=$buf.fgets($fp);
fclose($fp);
}
return $buf;
}

I have done a small project about it before. I used HTMLagilitypack.
Here is the source you can check it.
https://github.com/serdarb/Google-Results
https://github.com/serdarb/Google-Results/blob/master/frmMain.cs
HtmlAgilityPack.HtmlNodeCollection hrefList = htmlDoc.DocumentNode.SelectNodes("//div//ol//li[#class='g']//h3//a");
But do not send to many requests... Google asks for a captcha or returns error.

Related

Not able to read aliexpress.com via php

I'm trying to read aliexpress.com deals page via php. I'm not able to get the details of the page in output.
Is there a way I can get the details.
Below is the code.
<?php
include('simple_html_dom.php');
$url = 'http://activities.aliexpress.com/superdeals.php';
$xml = file_get_html($url);
//$file = 'output1.txt';
$element = $xml;
echo $element;
?>
This website used AJAX
There are two solutions :
- Make the same request as Javascript
- Using a tool like Phantomjs
If you look at requests, you will find easily that a GET request is made and return all information in JSON.
So you need to find by yourself the link or use third party library + tools.
EDIT:
You can use your web browser to get the link (I don't give you it because i think stackoverflow is not for that) with firebug or if you're using chrome, in network tab search for the JSON.
$url = "....";
$str = file_get_contents($url);
if($str) {
$json = json_decode($str, true); // json is an array
// ... do what you need
}
I recommend to use curl instead of file_get_contents for many reasons.
Or you can use Phantomjs (it's really more difficult) and get a "HTML snapshot" and then use DOM or XPATH to get what you need, but you must run Phantomjs and use a third party library for communicate with it.

PHP Google Maps Geolocation Request Failing Half Of The Time

I retrieved this function for making requests to Google Maps Geolocation API using PHP.
Make a request for The Google Maps Geolocation API
The issue I am having is the request fails around 50% of the time, and it takes forever to actually time out. ( Example: I'll make the request, and it will fail after about 20 seconds. I will refresh the page thus making the request again without changing any information, and it works ) I am trying to write this function into a class, but the service has been far too unreliable.
The Error:
Warning: file_get_contents(http://maps.google.com/maps/api/geocode/json?sensor=false&address=+chicago+IL): failed to open stream: Connection timed out in filename.php on line 2132
Is there any reason why?
Can I force this to timeout sooner?
Note:
HTACCESS is currently blocking all but my IP, but changing this doesn't seem to help
public function getGeoPosition($address){
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false" .
"&address=" . urlencode($address);
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
if($data['status']=="OK"){
return $data['results'];
}
}
print_r(getGeoPosition('chicago,il'));

Read Google Calendar API Push Notification in PHP

I have successfully configured Google API Calendar Event Change Push Notifications. My notification page gets called as it should be when I set up the notification or change the calendar.
BUT...being a bit of a PHP dunce I don't understand how to see the content of the notification.
SO (MAIN QUESTION)...how do I get at the variables in the notification? The notification calls my designated page. What are the variables? How are they sent? How are they organized?
There is the larger question of how one should determine the content of a call to a page in PHP. I tried all manner of var_dump, print_r, etc. All I get with:
<?php
$arr = get_defined_vars();
error_log(print_r($arr),0);
?>
is: 1 Yes, just the number 1 in my error_log.
The way I read the respose is:
$content = apache_request_headers();
If you print $content the data is self explenatory. However; for more information hwo to read the header check the link below:
https://developers.google.com/google-apps/calendar/v3/push#receiving
Thanks. it's working. i wrote to file.
$content = apache_request_headers();
get_contents($content);
function get_contents($result) {
$fp = fopen('config.json', 'w');
fwrite($fp, json_encode($result));
fclose($fp);
}

Google Maps API returns "Invalid Request" in a valid request

i am trying to reverse geocode a bunch of addresses for my database. I have about 200 addresses, and half of them worked like a charm.. i did put the google maps request in a loop and it worked. There were about 100 adresses that returned a "INVALID REQUEST" message. I echoed the request url and put it in my browser and it returned a "Status: OK" and all the data. Why does my script return a invalid request and another browser-tab returns OK?
Thats the function i used:
function getLatLon($strasse, $plz) {
$inhalt = simplexml_load_file('http://maps.googleapis.com/maps/api/geocode/xml?address='.$strasse.',+'.$plz.'&sensor=true');
$geo['lat'] = $inhalt->result->geometry->location->lat;
$geo['lon'] = $inhalt->result->geometry->location->lng;
return $geo;
}
The request url in clear looks like this:
http://maps.googleapis.com/maps/api/geocode/xml?address=Dreik%C3%B6nigstra%C3%9Fe+12,+Erlangen&sensor=true
Would be awesome if someone could help me or detect an error :)
Regards,
Philipp
I am pretty sure the error is caused by the german characters you are sending unencoded. When you do it by browser the URL will be encoded automatically, but not automatically by your PHP-script.
Use rawurlencode for this :
$inhalt = simplexml_load_file('http://maps.googleapis.com/maps/api/geocode/xml?'.
'address='.rawurlencode($strasse).',+'.
rawurlencode($plz).'&sensor=true');

PHP Proxy that handles both GET and POST without use of cURL

I've looked everywhere but can't find what I need so hence why I'm posting this question.
I'm looking for code for a PHP proxy to help me effectively do cross-domain jquery requests but have these requirements:
Must be PHP
Cannot use cURL -- my server config doesn't support it. Presumably this leaves fopen() but I'm open to alternatives.
Must support both GET and POST requests
Support both responses for either XML and JSON
I'e searched high and low for a solution but always find solutions that use cURL or doesn't support POST.
I'm currently using this which doesn't work for POST:
header('Content-type: application/json');
$daurl = $_GET['url'];
$handle = fopen($daurl, "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
file_get_contents(). See Example #4 at the linked page (PHP online docs) on how to do 'custom' requests using streams for POST and arbitrary HTTP headers.
in other for you to get quick answer, you first need to do some work yourself, then when you get stuck, you can post it here.
The question is too broad to be answered. you are requesting for a complete solution, you won't get it

Categories