1

Basically i want this json output to be transferred to my server/php.

i try this

$url="http://maps.google.com/maps/nav?q=from:9500 wil to:3000 bern";
$conte = file_get_contents($url);
echo $conte;

the json is not echo, how can i save the output to my server?

1
  • I get HTTP/1.0 400 Bad request with curl -I "http://maps.google.com/maps/nav?q=from:9500 wil to:3000 bern" Commented Dec 8, 2010 at 14:15

4 Answers 4

1

You need to urlencode the GET parameters:

echo file_get_contents('http://maps.google.com/maps/nav?q=from:9500%20wil%20to:3000%20bern');

# Returns
# {"name":"from:9500 wil to:3000 bern","Status":{"code":200,"request":"directions"},"Placemark":[{"id":"","address":"Wil, Switzerland","AddressDetails":{"Country":{"CountryNameCode":"CH","CountryName":"Schweiz","AdministrativeArea":{"AdministrativeAreaName":"St. Gallen","SubAdministrativeArea":{"SubAdministrativeAreaName":"Wil","Locality":{"LocalityName":"Wil"}}}},"Accuracy": 4},"Point":{"coordinates":[9.048081,47.463817,0]}},{"id":"","address":"Frohbergweg 7, 3012 Bern District, Switzerland","AddressDetails":{"Country":{"CountryNameCode":"CH","AdministrativeArea":{"AdministrativeAreaName":"BE","SubAdministrativeArea":{"SubAdministrativeAreaName":"Bern","Locality":{"LocalityName":"Bern District","DependentLocality":{"DependentLocalityName":"Länggasse-Felsenau","Thoroughfare":{"ThoroughfareName":"Frohbergweg 7"},"PostalCode":{"PostalCodeNumber":"3012"}}}}}},"Accuracy": 0},"Point":{"coordinates":[7.436386,46.954897,0]}}],"Directions":{"copyrightsHtml":"Map data \u0026#169;2010 Google, Tele Atlas ","summaryHtml":"178\u0026nbsp;km (about 2 hours 2 mins)","Distance":{"meters":177791,"html":"178\u0026nbsp;km"},"Duration":{"seconds":7343,"html":"2 hours 2 mins"},"Routes":[{"Distance":{"meters":177791,"html":"178\u0026nbsp;km"},"Duration":{"seconds":7343,"html":"2 hours 2 mins"},"summaryHtml":"178\u0026nbsp;km (about 2 hours 2 mins)","Steps":[{"descriptionHtml":"Head \u003Cb\u003Esouth\u003C\/b\u003E on \u003Cb\u003EToggenburgerstrasse\u003C\/b\u003E toward \u003Cb\u003ELerchenfeldstrasse\/\u003Cwbr\/\u003ERoute 16\/\u003Cwbr\/\u003ERoute 7\u003C\/b\u003E","Distance":{"meters":29,"html":"29\u0026nbsp;m"},"Duration":{"seconds":2,"html":"2 secs"},"Point":{"coordinates":[9.048030,47.463830,0]}},{"descriptionHtml":"Take the 1st left onto \u003Cb\u003ERoute 7\u003C\/b\u003E","Distance":{"meters":625,"html":"650\u0026nbsp;m"},"Duration":{"seconds":109,"html":"2 mins"},"Point":{"coordinates":[9.047930,47.463570,0]}},{"descriptionHtml":"At the traffic circle, take the \u003Cb\u003E1st\u003C\/b\u003E exit onto \u003Cb\u003EGeorg Rennerstrasse\u003C\/b\u003E","Distance":{"meters":871,"html":"850\u0026nbsp;m"},"Duration":{"seconds":77,"html":"1 min"},"Point":{"coordinates":[9.056170,47.463110,0]}},{"descriptionHtml":"Take the ramp to \u003Cb\u003EZürich\/\u003Cwbr\/\u003EFrauenfeld\u003C\/b\u003E","Distance":{"meters":330,"html":"350\u0026nbsp;m"},"Duration":{"seconds":22,"html":"22 secs"},"Point":{"coordinates":[9.053350,47.455800,0]}},{"descriptionHtml":"Merge onto \u003Cb\u003EA1\u003C\/b\u003E\u003Cdiv class=\"google_impnote\"\u003EToll road\u003C\/div\u003E","Distance":{"meters":173696,"html":"174\u0026nbsp;km"},"Duration":{"seconds":6790,"html":"1 hour 53 mins"},"Point":{"coordinates":[9.050270,47.453900,0]}},{"descriptionHtml":"Take exit \u003Cb\u003E36-Bern-Neufeld\u003C\/b\u003E toward \u003Cb\u003EBremgarten\u003C\/b\u003E","Distance":{"meters":579,"html":"600\u0026nbsp;m"},"Duration":{"seconds":33,"html":"33 secs"},"Point":{"coordinates":[7.436980,46.966570,0]}},{"descriptionHtml":"At the traffic circle, take the \u003Cb\u003E2nd\u003C\/b\u003E exit onto \u003Cb\u003ENeubrückstrasse\u003C\/b\u003E","Distance":{"meters":1357,"html":"1.4\u0026nbsp;km"},"Duration":{"seconds":243,"html":"4 mins"},"Point":{"coordinates":[7.429580,46.966790,0]}},{"descriptionHtml":"Turn right at \u003Cb\u003EMittelstrasse\u003C\/b\u003E","Distance":{"meters":146,"html":"150\u0026nbsp;m"},"Duration":{"seconds":24,"html":"24 secs"},"Point":{"coordinates":[7.437750,46.956720,0]}},{"descriptionHtml":"Take the 1st left onto \u003Cb\u003EBrückfeldstrasse\u003C\/b\u003E","Distance":{"meters":104,"html":"100\u0026nbsp;m"},"Duration":{"seconds":33,"html":"33 secs"},"Point":{"coordinates":[7.436060,46.956100,0]}},{"descriptionHtml":"Take the 1st right onto \u003Cb\u003EFrohbergweg\u003C\/b\u003E\u003Cdiv class=\"google_note\"\u003EDestination will be on the left\u003C\/div\u003E","Distance":{"meters":54,"html":"54\u0026nbsp;m"},"Duration":{"seconds":10,"html":"10 secs"},"Point":{"coordinates":[7.436830,46.955320,0]}}],"End":{"coordinates":[7.436234,46.955057,0]}}]}}
Sign up to request clarification or add additional context in comments.

2 Comments

+1. I figured it out as I edited the OP, so I posted it anyway with my answer. Hope you don't mind.
@alex - No problem, i was also reaching for the fopen URL wrappers at first when I tried curl, and it didn't work either - It had to be the parameters.
1

How do you want to save it? As a file?

If you can open via file_get_contents(), then URL opening for fopen() wrappers is on.

Then you can just do...

$url = 'http://maps.google.com/maps/nav?q=from:9500 wil to:3000 bern';

$content = file_get_contents($url);

file_put_contents('google-map-json.txt', $content);

You can get that into a usable object in PHP with json_decode().

You may want to do that if you want to save it to your database.

If you don't want to overwrite the file each time, you could generate a random hash of the response for the filename, or something similar.

Update

sorry my bad. i know how to save file. but the json is not even echoed through file_get_contents.

You may not have URL fopen() wrappers enabled.

You can find out by running this...

var_dump(ini_get('allow_url_fopen'));

If it is disabled, and you can't or don't want to turn it on, you can use cURL (if you have that library installed).

Update

When I tried to access the page via file_get_contents(), I got...

HTTP/1.0 400 Bad Request

You may need to use cURL, and mimic a browser (user agent etc).

Or you can set

ini_set('user_agent', 'Mozilla or something');

And then use file_get_contents().

Update

I tried with cURL too, and it didn't work :(

I think the next step is to examine all the headers your browser sends (when it works), and then send the equivalent via cURL.

Update

I noticed the Markdown editor wasn't liking the URL (see my OP's edit), and it dawned me - urlencode() the GET params!

4 Comments

sorry my bad. i know how to save file. but the json is not even echoed through file_get_contents.
im on hostgator so it works. but the code does not work, try it yourself. i can't write or echo that output with php. i would know how to do with Javascript but thats not the question.
@Email Hmm, the URL fetching works for me on my Hostgator account. What does var_dump(file_get_contents('http://maps.google.com/maps/nav?q=from:9500 wil to:3000 bern')) output? Is it possible they block user agents, or your IP? Has it worked in the past? What did that update code output above?
thx 4your help. it really was the urlencode like jwandborg stated. +1
0

You can write the data to a file using PHP's IO functions

$fp = fopen('data.txt', 'a');
fwrite($fp, $conte);
fclose($fp);

1 Comment

the file is empty since even the echoing of that data fails.
0

You can use json_decode to parse the data from the file_get_contents() variable. (I would personally use cURL instead of file_get_contents()).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.