Skip to content

Instantly share code, notes, and snippets.

@oscarvalenzuelab
Created November 23, 2019 06:05
Show Gist options
  • Save oscarvalenzuelab/6bfb7a75b6879d5e70ddc5f54ed4fb97 to your computer and use it in GitHub Desktop.
Save oscarvalenzuelab/6bfb7a75b6879d5e70ddc5f54ed4fb97 to your computer and use it in GitHub Desktop.
OpenDataCollector PHP Client Example using Curl authenticated using OAUTH-2.0
<?php
//ODC Input Settings
$odcApi = "cars";
$odcInput = "XD3500";
//ODC Account Settings
$odcClientId = "";
$odcClientSecret = "";
//ODC Server Settings
$odcServer = "dataserver.opendatacollector.com";
$odcVersion = "v1";
$odcProto = "https";
$odcBaseUrl = $odcProto."://".$odcServer."/api/".$odcVersion;
//Initiate CURL Client
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//Getting the OAUTH-2.0 Token
curl_setopt($ch, CURLOPT_URL, $odcBaseUrl."/oauth/token");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'client_id' => $odcClientId,
'client_secret' => $odcClientSecret,
'scope' => '*',
'grant_type' => 'client_credentials'
));
$data = curl_exec($ch);
$jsonData = json_decode($data,1);
//If Token then query the API
if(array_key_exists('access_token', $jsonData)){
$odcToken = $jsonData['access_token'];
$header = array("Authorization: Bearer {$odcToken}");
//ODC query the endpoint
$odcPlate = "CFZP61";
$odcApi = "/".$odcApi."/".$odcInput;
curl_setopt($ch, CURLOPT_URL, $odcBaseUrl.$odcApi);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($ch);
var_dump($data);
}
curl_close($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment