<?php // demo/temp_lvmllc.php
error_reporting(E_ALL);
/**
* SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28577388.html
*/
$api = 'http://api.census.gov/data/2010/sf1?key=e2f9c8e47bda2c47d65d27e3e671fbaa52e8c60d&get=P0010001,NAME&for=county&in=state:19';
$jso = my_curl($api);
$arr = json_decode($jso);
// ACTIVATE THIS TO SEE THE ENTIRE DATA SET
// var_dump($arr);
// FORMAT TABULAR DATA
$out = '<table>' . PHP_EOL;
// ACQUIRE THE HEADERS
$hed = $arr[0];
array_shift($arr);
// ADD HEADERS TO TABLE
$out .= <<<EOD
<tr>
<th>{$hed[0]}</th>
<th>{$hed[1]}</th>
<th>{$hed[2]}</th>
<th>{$hed[3]}</th>
</tr>
EOD;
// ADD THE ROWS TO THE TABLE
foreach ($arr as $row)
{
$out .= <<<EOD
<tr>
<td>{$row[0]}</td>
<td>{$row[1]}</td>
<td>{$row[2]}</td>
<td>{$row[3]}</td>
</tr>
EOD;
}
// TIE OFF THE TABLE AND SHOW THE WORK PRODUCT
$out .= '</table>' . PHP_EOL;
echo $out;
// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=5
, $error_report=TRUE
)
{
$curl = curl_init();
// HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK
// SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0' );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $header );
curl_setopt( $curl, CURLOPT_REFERER, 'http://www.google.com' );
curl_setopt( $curl, CURLOPT_ENCODING, 'gzip,deflate' );
curl_setopt( $curl, CURLOPT_AUTOREFERER, TRUE );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE );
curl_setopt( $curl, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $curl, CURLOPT_VERBOSE, TRUE );
curl_setopt( $curl, CURLOPT_FAILONERROR, TRUE );
// IF USING SSL, THESE MAY BE IMPORTANT
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $curl, CURLOPT_SSLVERSION, 3 );
// RUN THE CURL REQUEST AND GET THE RESULTS
$htm = curl_exec($curl);
// ON FAILURE
if ($htm === FALSE)
{
// VISUALIZE ERROR MESSAGES
if ($error_report)
{
$err = curl_errno($curl);
$inf = curl_getinfo($curl);
echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
var_dump($inf);
}
curl_close($curl);
return FALSE;
}
// ON SUCCESS RETURN XML / HTML STRING
curl_close($curl);
return $htm;
}
foreach($file as $row){
echo $row['name'] . " - " . $row['state'] . "<br>";
}
$api = 'http://api.census.gov/data/2010/sf1?key=e2f9c8e47bda2c47d65d27e3e671fbaa52e8c60d&get=P0010001,NAME&for=county:*&in=state:19';
$file = file_get_contents($api);
$file=json_decode($file);
//var_dump($file);
foreach($file as $row){
echo $row['name'] . " - " . $row['state'] . "<br>";
}
Open in new window