Created
May 31, 2012 13:47
-
-
Save phillipsj/2843519 to your computer and use it in GitHub Desktop.
Example of working with Socrata API using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Url for column metadata | |
$urlColumns = "http://data.austintexas.gov/api/views/ecmv-9xxi/columns.json"; | |
$curlColumns = curl_init($urlColumns); | |
curl_setopt($curlColumns, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-App-Token: GQXtcmDiC3Qjcoys9iwCI8esO', 'Content-Type: application/json')); | |
$json_columns = curl_exec($curlColumns); | |
$status = curl_getinfo($curlColumns, CURLINFO_HTTP_CODE); | |
curl_close($curlColumns); | |
$columns = json_decode($json_columns, true); | |
$dateColumn = ""; | |
//Get id of column for date time since the id changes every time new data is published. | |
foreach($columns as $column){ | |
if($column['fieldName'] == 'inspection_date'){ | |
$dateColumn = $column['id']; | |
} | |
} | |
$url2 = "http://data.austintexas.gov/api/views/INLINE/rows.json?method=getRows&start=0&length=1000"; | |
$content = '{"originalViewId":"ecmv-9xxi","name":"Range","query":{"filterCondition":{"type":"operator","value":"BETWEEN","children":[{"columnId":'. $dateColumn .',"type":"column"},{"type":"literal","value":"05/01/2012"},{"type":"literal","value":"05/03/2012"}]}}}'; | |
$curl = curl_init($url2); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-App-Token: <insert your app token>', 'Content-Type: application/json', 'Content-Length: ' . strlen($content))); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $content); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); | |
$json_response = curl_exec($curl); | |
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
#if ( $status != 201 ) { | |
# die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl)); | |
#} | |
curl_close($curl); | |
$response = json_decode($json_response, true); | |
echo count($response); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This examples shows how to use the SODA API to query the restaurant score data by a date range.