Skip to content

Instantly share code, notes, and snippets.

@kingkool68
Last active August 29, 2015 14:16
Show Gist options
  • Save kingkool68/864a087b0b10cffde677 to your computer and use it in GitHub Desktop.
Save kingkool68/864a087b0b10cffde677 to your computer and use it in GitHub Desktop.
Basic GET request from Uber's API
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
$url = 'https://api.uber.com/v1/estimates/price';
$args = array(
/*
'headers' => array(
'Authorization' => $some_token_goes_here
),
*/
'body' => array(
'server_token' => 'spKIM1CliT6oUUdl4whd0Q_JFhOva9AzMplxEaR2',
'start_latitude' => 39.331111,
'start_longitude' => -76.632658,
'end_latitude' => 39.331111,
'end_longitude' => -76.632658
)
);
//Send off the request to the API passing our arguments. Store the returned response as $result
//$result = wp_remote_get( $url, $args );
//Probably should do some checks here to see if it is a valid response or if there was an error. But whatevs...
/*
While debugging we can see everything in $result by using var_dump() The <pre> tags make it look pretty in the browser.
echo '<pre>';
var_dump( $result );
echo '</pre>';
*/
//The result will probably come back as a string that needs to be turned into arrays/objects in PHP that we can work with
//$the_json = json_decode( $result['body'] );
//Let's see what is in $the_json
//echo '<pre>';
//var_dump( $the_json );
//echo '</pre>';
//Once you figure out what fields you actually want you can process $the_json and then do something with it like loop over each result and store parts of it in a database.
//See $wpdb documentation for running SQL queries to add data to a db ==> http://codex.wordpress.org/Class_Reference/wpdb
/* Another example */
$url = 'https://www.uber.com/fares/latLngEstimate?start_latitude=19.3889059&start_longitude=-99.2518834&end_latitude=19.3969138&end_longitude=-99.18928140000003&vids[]=651&vids[]=188&vids[]=434&city=mexico_city';
$args = array();
$result = wp_remote_get( $url, $args );
$the_json = json_decode( $result['body'] );
echo '<pre>';
//var_dump( $the_json );
echo '</pre>';
?>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Fare</th>
</tr>
</thead>
<tbody>
<?php foreach( $the_json->estimates as $row ): ?>
<tr>
<td><?php echo $row->vehicle_view->display_name; ?></td>
<td><?php echo $row->fare_string; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment