Skip to content

Instantly share code, notes, and snippets.

@WilliamGarrow
Last active October 27, 2015 14:47
Show Gist options
  • Save WilliamGarrow/e0f18a3f292554cfdee3 to your computer and use it in GitHub Desktop.
Save WilliamGarrow/e0f18a3f292554cfdee3 to your computer and use it in GitHub Desktop.
PHP - REST API and JSON exercise using Google Maps and Instagram
<?php
if (!empty($_GET['location'])){
$maps_url = 'https://'.
'maps.googleapis.com/'.
'maps/api/geocode/json'.
'?address=' . urlencode($_GET['location']);
$maps_json = file_get_contents($maps_url);
$maps_array = json_decode($maps_json, true);
$lat = $maps_array['results'][0]['geometry']['location']['lat'];
$lng = $maps_array['results'][0]['geometry']['location']['lng'];
$instagram_url = 'https://'.
'api.instagram.com/v1/media/search' .
'?lat=' . $lat .
'&lng=' . $lng .
'&client_id=CLIENT-ID';
$instagram_json = file_get_contents($instagram_url);
$instagram_array = json_decode($instagram_json, true);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>REST API Exercise</title>
</head>
<body>
<form action="/geogram.php" method="get">
<input type="text" name="location"/>
<button type="submit">Submit</button>
</form>
<br/>
<?php
if(!empty($instagram_array)){
foreach($instagram_array['data'] as $key=>$image){
echo '<img src="'.$image['images']['low_resolution']['url'].'" alt=""/><br/>';
}
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment