Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zmughal/aac5359463dc939a2955 to your computer and use it in GitHub Desktop.
Save zmughal/aac5359463dc939a2955 to your computer and use it in GitHub Desktop.
<?php
$apiUrl = "https://www.googleapis.com/civicinfo/us_v1/representatives/lookup?fields=officials&key=AIzaSyARokMXd73Cj3k0rXRFh-s689q_uprujTY";
// $json_url = $apiUrl;
//
// $json = file_get_contents($json_url);
//
// $data = json_decode($json,true);
//
//
// // Result: null
//
// class User {
// public $address = "11038 Kirkridge Dr. Houston Texas";
// }
//
// $user = new User();
//
//
// $url = $apiUrl;
// $content = json_encode($user);
//
// echo $content;
// echo "<br>";
//
// $curl = curl_init($url);
// curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
// curl_setopt($curl, CURLOPT_HEADER, false);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($curl, CURLOPT_HTTPHEADER,
// array("Content-type: application/json", "X-JavaScript-User-Agent: Google APIs Explorer"));
// curl_setopt($curl, CURLOPT_POST, true);
// curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
//
// $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);
// The data to send to the API
$postData = array(
'address' => '11038 Kirkrdige Dr. Houston Tx',
);
// Setup cURL
$ch = curl_init($apiUrl);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'X-JavaScript-User-Agent: Google APIs Explorer'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
foreach($responseData['offices'] as $val){
echo "ID: ".$val['officials']."<br>";
echo "Name: ".$val['name']."<br>";
echo "Party: ".$val['party']."<br><br>";
}
foreach($responseData['officials'] as $val){
echo "ID: ".$val['officials']."<br>";
echo "Name: ".$val['name']."<br>";
echo "Party: ".$val['party']."<br><br>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment