i am looking for some help to find the distance between two address through php
as i can not use the google maps api. so get a way from this post
Distance between two addresses
but i need to know how can send the request using URL pattern
and grab the repose to save them in database.
http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false
thanks for any suggestion.
///////////////
After these answers i am there
$customer_address_url = urlencode($customer_address);
$merchant_address_url = urlencode($merchant_address);
$map_url = "http://maps.google.com/maps/api/directions/xml?origin=".$merchant_address_url."&destination=".$customer_address_url."&sensor=false";
$response_xml_data = file_get_contents($map_url);
$data = simplexml_load_string($response_xml_data);
XML response is visible when i put this url : http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York%2C+New+York%2C+10001+United+States&sensor=false
but can not printing through
echo "<pre>"; print_r($data); exit;
You can try this..
$url = "http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false";
$data = file_get_contents($url);
Now $data contains resulting xml data. You can parse this xml data using..
http://php.net/manual/simplexml.examples.php
use the haversine formula
Also check https://developers.google.com/maps/documentation/distancematrix/
Related
<?php
$url = "http://www.justdial.com/Delhi-NCR/Pizza-Outlets-%3Cnear%3E-Okhla/";
$ptr = fopen("op.txt","w");
$data = file_get_contents($url);
print_r($data);
$result = htmlentities($data);
$doc = new DOMDocument();
#$doc->loadHTML($result);
$finder = new DOMXPath($doc);
$node = $finder->query("//h3[contains(#class, 'r')]");
?>
Above is the code which I have written to fetch the source code of justdial. The only output which I get is the first pizza outlet.How can I fetch all the results which are shown on the justdial website.
Thanks in advance.
All items are part of the '<div id="tab_block">' html element which its content is built by javascript/AJAX calls, so they cannot appear in the HTML file you load via file_get_contents() since you will get the HTML definition only without the javascript code being interpreted.
However, it means you can access the items/database directly by code if you know the endpoints.
For example (url are shown as when I tested them)
This url will return the first few items of the complete list. It will return something like (in JSON format):
[{docid: "011PXX11.XX11.151106170721.W5H9",…}, {docid: "011PXX11.XX11.140302105210.Y9N8",…},…]
0: {docid: "011PXX11.XX11.151106170721.W5H9",…}
disp_pic: "http://images.jdmagicbox.com/delhi/h9/011pxx11.xx11.151106170721.w5h9/catalogue/6cf575ffbd1090f5a314d2cf40451c88.jpg"
docid: "011PXX11.XX11.151106170721.W5H9"
1: {docid: "011PXX11.XX11.140302105210.Y9N8",…}
disp_pic: "http://images.jdmagicbox.com/delhi/n8/011pxx11.xx11.140302105210.y9n8/catalogue/ecfd2106644df17013e98bb60f40c527.jpg"
docid: "011PXX11.XX11.140302105210.Y9N8"
video: "http://videos.jdmagicbox.com/delhi/n8/011pxx11.xx11.140302105210.y9n8/video/fc2a62242ae03c74c15436dbcc04c33a_m.jpg"
...
the docid can be used to do further query on a particular item, while the disp_pic url will return the image
This url will return the image of the 1st item too but use some parameters
In any case, I just scratch the surface of the whole issue to demonstrate how to proceed. You would need to understand the site logic to read the complete dataset, but it would be easier to contact the webmaster and ask him to describe its API/endpoints for you to access the data. As well as asking him permission to use it even if the 'API' is not protected.
Once you know the endpoint, structure and data description, you can use a PHP library like mashape\unirest to do queries like this:
Unirest\Request::verifyPeer (false) ;
$response =Unirest\Request::get (
'http://www.justdial.com/functions/sortbyphotosnew.php?contractid=011PXX11.XX11.151106170721.W...,
array ( 'Accept' => 'application/json' ),
null
) ;
if $response->code == 200 then the $response->body is your JSON object containing the document array.
Hi I have never used xml but need to now, so I am trying to quickly learn but struggling with the structure I think. This is just to display the weather at the top of someones website.
I want to display Melbourne weather using this xml link ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10753.xml
Basically I am trying get Melbourne forecast for 3 days (what ever just something that works) there is a forecast-period array [0] to [6]
I used this print_r to view the structure:
$url = "linkhere";
$xml = simplexml_load_file($url);
echo "<pre>";
print_r($xml);
and tried this just to get something:
$url = "linkhere";
$xml = simplexml_load_file($url);
$data = (string) $xml->forecast->area[52]->description;
echo $data;
Which gave me nothing (expected 'Melbourne'), obviously I need to learn and I am but if someone could help that would be great.
Because description is an attribute of <area>, you need to use
$data = (string) $xml->forecast->area[52]['description'];
I also wouldn't rely on Melbourne being the 52nd area node (though this is really up to the data maintainers). I'd go by its aac attribute as this appears to be unique, eg
$search = $xml->xpath('forecast/area[#aac="VIC_PT042"]');
if (count($search)) {
$melbourne = $search[0];
echo $melbourne['description'];
}
This is a working example for you:
<?php
$forecastdata = simplexml_load_file('ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10753.xml','SimpleXMLElement',LIBXML_NOCDATA);
foreach($forecastdata->forecast->area as $singleregion) {
$area = $singleregion['description'];
$weather = $singleregion->{'forecast-period'}->text;
echo $area.': '.$weather.'<hr />';
}
?>
You can edit the aforementioned example to extract the tags and attributes you want.
Always remember that a good practice to understand the structure of your XML object is printing out its content using, for instance, print_r
In the specific case of the XML you proposed, cities are specified through attributes (description). For this reason you have to read also those attributes using ['attribute name'] (see here for more information).
Notice also that the tag {'forecast-period'} is wrapped in curly brackets cause it contains a hyphen, and otherwise it wouldn generate an error.
I have this code that gets the IP address and Latitude and Longitude of the user. However I am wanting ti display it on a map if it finds the lat and long.
$theirip = $_SERVER['REMOTE_ADDR'];
$fileinfo = file_get_contents("http://api.easyjquery.com/ips/?ip=".$theirip."&full=true");
This outputs a string like this.
{"IP":"000.000.00.000","continentCode":"Unknown","continentName":"Unknown","countryCode2":"Unknown","COUNTRY":"Unknown","countryCode3":"Unknown","countryName":"Unknown","regionName":"Unknown","cityName":"Unknown","cityLatitude":0,"cityLongitude":0,"countryLatitude":0,"countryLongitude":0,"localTimeZone":"Unknown","localTime":"0"}
I am wanting to retrieve just the cityLatitude and cityLongitude from this. I have researched some and tried some different code.
$lat = $fileinfo -> {"cityLatitude"};
but i'm not quite sure how to go about this. Any help would be appreciated!
What you have there is a JSON string, you can use json_decode to pasre it
$obj = json_decode($fileinfo);
$lat = $obj -> {"cityLatitude"};
its a JSONObject... just use the json_decode() to get data from it
no need to decode the JSON, it should work fine.
trying from here its working just fine...
try using your IP instead of $theirip = $_SERVER['REMOTE_ADDR'];
You're using PHP right? It should be
$info = json_decode($fileinfo);
$citylatitude = $info->cityLatitude;
http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json
is the url, which contains json data. I want to use that data and send SMS to a particular phone no if value of latitude and longitude(Extracted from JSON).
Check constraints, we can use through php. But the main problem is How to extract data from JSON file?
I don't want to give you the solution, so the below should be enough to get you started.
Read in the JSON data using file_get_contents
Parse the JSON string into a PHP object using json_decode
Your code should look something like this:
$url = "http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json";
$contents = file_get_contents($url);
$jsonObj = json_decode($contents);
You mean something like this?
<?php
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->trends as $trend )
{
echo "{$trend->name}\n";
}
I need to find the number of indexed pages in google for a specific domain name, how do we do that through a PHP script?
So,
foreach ($allresponseresults as $responseresult)
{
$result[] = array(
'url' => $responseresult['url'],
'title' => $responseresult['title'],
'abstract' => $responseresult['content'],
);
}
what do i add for the estimated number of results and how do i do that?
i know it is (estimatedResultCount) but how do i add that? and i call the title for example this way: $result['title'] so how to get the number and how to print the number?
Thank you :)
I think it would be nicer to Google to use their RESTful Search API. See this URL for an example call:
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:stackoverflow.com&filter=0
(You're interested in the estimatedResultCount value)
In PHP you can use file_get_contents to get the data and json_decode to parse it.
You can find documentation here:
http://code.google.com/apis/ajaxsearch/documentation/#fonje
Example
Warning: The following code does not have any kind of error checking on the response!
function getGoogleCount($domain) {
$content = file_get_contents('http://ajax.googleapis.com/ajax/services/' .
'search/web?v=1.0&filter=0&q=site:' . urlencode($domain));
$data = json_decode($content);
return intval($data->responseData->cursor->estimatedResultCount);
}
echo getGoogleCount('stackoverflow.com');
You'd load http://www.google.com/search?q=domaingoeshere.com with cURL and then parse the file looking for the results <p id="resultStats" bit.
You'd have the resulting html stored in a variable $html and then say something like
$arr = explode('<p id="resultStats"'>, $html);
$bottom = $arr[1];
$middle = explode('</p>', $bottom);
Please note that this is untested and a very rough example. You'd be better off parsing the html with a dedicated parser or matching the line with regular expressions.
google ajax api estimatedResultCount values doesn't give the right value.
And trying to parse html result is not a good way because google blocks after several search.
Count the number of results for site:yourdomainhere.com - stackoverflow.com has about 830k
// This will give you the count what you see on search result on web page,
//this code will give you the HTML content from file_get_contents
header('Content-Type: text/plain');
$url = "https://www.google.com/search?q=your url";
$html = file_get_contents($url);
if (FALSE === $html) {
throw new Exception(sprintf('Failed to open HTTP URL "%s".', $url));
}
$arr = explode('<div class="sd" id="resultStats">', $html);
$bottom = $arr[1];
$middle = explode('</div>', $bottom);
echo $middle[0];
Output:
About 8,130 results
//vKj
Case 2: you can also use google api, but its count is different:
https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=ursitename&callback=processResults
https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:google.com
cursor":{"resultCount":"111,000,000","
"estimatedResultCount":"111000000",