How to Properly Convert String to Associative Array in PHP - php

i want to convert this string to Proper associative array.
please click on the link to see string data.
https://maps.googleapis.com/maps/api/geocode/json?latlng=16.539311299999998,80.5820222
i want that to be converted into a proper Associaitve array.
im looking something like this.
echo $array['long_name'] should return a value like 'Attlee Road'
please help me with this, im trying this from 2 days, trying all explode, implode, foreachloops, im confused a lot, please someone help me with this.
Below code will echo the string data
<?php
$lat = 16.539311299999998;
$lng = 80.5820222;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $lng . ";
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
echo "<pre>";
print_r($returned_contents);
echo "</pre>";
?>

You must be use json_decode() function.
<?php
$lat = 16.539311299999998;
$lng = 80.5820222;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $lng . "&key=AIzaSyDCgyjLTrpadabEAyDNXf2GPpgChvpMw_4";
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
$returned_contents_array = json_decode($returned_contents, true);
$results = $returned_contents_array['results'];
foreach($results as $result) {
echo $result['address_components'][0]['long_name'];
}
?>

Use json_decode() on your output

Try it
<?php
$lat = 16.539311299999998;
$lng = 80.5820222;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $lng . "&key=AIzaSyDCgyjLTrpadabEAyDNXf2GPpgChvpMw_4";
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
$returned_contents = json_decode($returned_contents);
echo "<pre>";
print_r($returned_contents);
echo "</pre>";
?>

Related

Trying Convert Address to Lat Lang but Giving me error Over_Query_Limit in PHP

Actually I want to get lat lang from address through google api and then using that lat lang i wanna get distance with my current lat lang which right know I have hard coded but its giving me error of Over_Query_Limit.
I tried different way but did not work. Please guide me how can I avoid from this problem.
This is my PHP Script code:
<?php
$address = 'iqra university islamabad';
$url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false");
sleep(2);
$response = json_decode($url);
if ($response->status == 'OK') {
$latitude = $response->results[0]->geometry->location->lat;
$longitude = $response->results[0]->geometry->location->lng;
GetDrivingDistance(33.651936,$latitude,73.064366, $longitude);
echo '<br />';
echo 'Latitude: ' . $latitude;
echo '<br />';
echo 'Longitude: ' . $longitude;
} else {
echo $response->status;
}
function GetDrivingDistance($lat1, $lat2, $long1, $long2)
{
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
echo 'distance: ' . $dist;
//return array($dist,$time);
}
?>

php json_decode is not working Properly

When i am Decoding using commented "$jsonString" String it is working very well.
But after using curl it is not working, showing Null.
Please Help Me in this.
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
$url = 'http://ExampleStatus.php?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// $jsonString = '[{"branchname":"BHUBNESHWAR","consignee":"ICICI BANK LTD","currentstatus":"Delivered by : BHUBNESHWAR On - 25/07/2015 01:00","dlyflag":"Y","PODuploaded":"Not Uploaded"}]';
if ($jsonString != '') {
$json = str_replace(array('[', ']'), '', $jsonString);
echo $json;
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}
}
}
After curl I used following concept to get only JSON data from HTML Page.
1) fetchData.php
$url = 'http://DocketStatusApp.aspx?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// now get only value
$dom = new DOMDocument();
$dom->loadHTML($jsonString);
$thediv = $dom->getElementById('Label1');
echo $thediv->textContent;
2) JSONprocess.php
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
ob_start(); // begin collecting output
include_once 'fetchData.php';
$result = ob_get_clean(); // Completed collecting output
// Now it will show & take only JSON Data from Div Tag
$json = str_replace(array('[', ']'), '', $result);
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}

How to echo the cURL result in PHP?

Here is a URL→ https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=snoopy&rsz=1
and this is my php code
<?php
$url = "https://ajax.googleapis.com/ajax/services/search/images?"."v=1.0&q=snoopy";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
?>
I wanna echo all of the image urls from its result
(For example: http://img2.wikia.nocookie.net/__cb20110331075248/peanuts/images/6/62/Snoopy.gif )
But I have no idea how to echo them.
Hope someone could help me, thanks!
Maybe like this:
<?php
$url = "https://ajax.googleapis.com/ajax/services/search/images?" .
"v=1.0&q=barack%20obama&userip=INSERT-USER-IP";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://example.com');
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$pics = json_decode($body);
$ret='';
if($pics){
foreach($pics->responseData->results as $pic)
$ret .= $pic->url.'<br>';
}
echo $ret;
?>
in order to echo the exact result just use:
echo $body; //echoes json string
if you want to echo the exact link, echo like this:
echo $json->responseData->results[0]->url; //echoes http://img2.wikia.nocookie.net/__cb20110331075248/peanuts/images/6/62/Snoopy.gif
or you can foreach() twice (or once...) then echo $res->url directly :) your choise!

getting Latitude And Longitude From Pin Code Using PHP

In my site i want to get the latitude and longitude of given pin code or postal code.
If i give 680721 as pin code or postal code then i want to get its corresponding latitude and longitude using php code.
How can i do this?
If this is possible in PHP?
I have php code to get latitude and longitude of given name of place.
$Url='http://maps.googleapis.com/maps/api/geocode/json?address='.$exp_pjctloc[$i].'&sensor=false';
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$search_data = json_decode($output);
$lat = $search_data->results[0]->geometry->location->lat;
$lng = $search_data->results[0]->geometry->location->lng;
But how can i get it by using pin code or postal code?
Below code worked for me
<?php
$zipcode="92604";
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false";
$details=file_get_contents($url);
$result = json_decode($details,true);
$lat=$result['results'][0]['geometry']['location']['lat'];
$lng=$result['results'][0]['geometry']['location']['lng'];
echo "Latitude :" .$lat;
echo '<br>';
echo "Longitude :" .$lng;
?>
This worked for me:
$zip = 94043;
$site = file_get_contents('http://geocoder.ca/?postal='.$zip, false, NULL, 1000, 1000);
$goods = strstr($site, 'GPoint('); // cut off the first part up until
$end = strpos($goods, ')'); // the ending parenthesis of the coordinate
$cords = substr($goods, 7, $end - 7); // returns string with only the
$array = explode(', ',$cords); // convert string into array
echo "<pre>";
print_r($array); // output the array to verify
With the curl you can get lat and long from below code:
$zipcode=90012;
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zipcode)."&sensor=false&key=XXXXXXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$search_data = json_decode($output);
$lat = $search_data->results[0]->geometry->location->lat;
$lng = $search_data->results[0]->geometry->location->lng;

Yahoo indexed pages script

I'm using following script:
<?php
$domain_name = 'davidwinstead.com';
$domain_name = strtolower(trim($domain_name));
$yahoo_url = 'http://siteexplorer.search.yahoo.com/search?p=http%3A%2F%2F'.$domain_name;
$yahoo_url_contents = get_yahoo_contents($yahoo_url);
if(preg_match('/Pages \(([0-9,]{1,})\)/im', $yahoo_url_contents, $regs)){
$indexed_pages = trim($regs[1]);
echo ucwords($domain_name).' Has <u>'.$indexed_pages.'</u> Pages Indexed # Yahoo.com';
}else{
echo ucwords($domain_name).' Has Not Been Indexed # Yahoo.com!';
}
function get_yahoo_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
but appear don't working
anyone have any working script?
thanks
<?php
$domain_name = 'davidwinstead.com';
$domain_name = strtolower(trim($domain_name));
$yahoo_url = 'http://siteexplorer.search.yahoo.com/search?p=http%3A%2F%2F'.$domain_name;
$yahoo_url_contents = file_get_contents($yahoo_url);
if(preg_match('/Pages \(([0-9,]{1,})\)/im', $yahoo_url_contents, $regs)){
$indexed_pages = trim($regs[1]);
echo ucwords($domain_name).' Has <u>'.$indexed_pages.'</u> Pages Indexed # Yahoo.com';
}else{
echo ucwords($domain_name).' Has Not Been Indexed # Yahoo.com!';
} ?>

Categories