Solved

PHP Retrieve values from external page

Posted on 2014-12-09
11
68 Views
Last Modified: 2014-12-15
I need to use PHP to retrieve values from the US Census API.

The URL I want to retrieve the info from is

http://api.census.gov/data/2010/sf1?key=e2f9c8e47bda2c47d65d27e3e671fbaa52e8c60d&get=P0010001,NAME&for=county&in=state:19

Once retrieved I need to parse the result into an array.

I have done this all with JS before, but this project needs everything to happen in just PHP.

Thanks
0
Comment
Question by:lvmllc
  • 5
  • 4
  • 2
11 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40489835
<?php
$file = file_get_contents('http://api.census.gov/data/2010/sf1?key=e2f9c8e47bda2c47d65d27e3e671fbaa52e8c60d&get=P0010001,NAME&for=county&in=state:19');

$file=json_decode($file);

var_dump($file);

Open in new window

0
 

Author Comment

by:lvmllc
ID: 40489888
It seems to stall out on the $file ...  even when I comment out the last two lines.

I also tried update URL to http://api.census.gov/data/2010/sf1?key=e2f9c8e47bda2c47d65d27e3e671fbaa52e8c60d&get=P0010001,NAME&for=county:*&in=state:19
0
 
LVL 58

Expert Comment

by:Gary
ID: 40489909
Try it again, the site was not responding for me a few moments ago.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40490077
For some reason it seems to respond faster when using cURL.  This may just be coincidence, tho.

Please see: http://iconoun.com/demo/temp_lvmllc.php

<?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;
}

Open in new window

0
 

Author Comment

by:lvmllc
ID: 40491303
Gary,
I can now dump the file to screen.  But what if I want to retrieve and print just one of the values? How is this stored in the Array?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:lvmllc
ID: 40491341
Ray,
I see how your code works. Is it possible to just use CURL instead of myCurl? Apparently our server host does not have that enabled, but does have CURL
0
 
LVL 58

Expert Comment

by:Gary
ID: 40491372
The returned data is just rows of the name, state etc
I'm not sure what exactly you want to extract, for example to loop it and get the name and state

foreach($file as $row){
   echo $row['name'] . " - " . $row['state'] . "<br>";
}

Open in new window

0
 

Author Comment

by:lvmllc
ID: 40491529
I have the following but I get and Undefined index: name in  error and it just prints the -



$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

0
 
LVL 58

Accepted Solution

by:
Gary earned 450 total points
ID: 40491554
0 - P0010001
1 - Name
2 - State
3 - County
<?php
$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[0] . " - " . $row[1] . " - " .  $row[2] . " - " . $row[3] . "<br>";
}

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 40495412
Is it possible to just use CURL instead of myCurl? Apparently our server host does not have that enabled, but does have CURL
"myCurl" is the name of a local function that wraps a call to the cURL engine. The function provides settings and default values for cURL that are almost certainly what you want.  If you have not tried running the code exactly as I posted it here, please do so, then post back if you still have any questions or need help with the details.


If you're new to PHP and need some learning resources to get started, this article may be able to help:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 

Author Closing Comment

by:lvmllc
ID: 40500238
Gary - your solution has worked perfect in my situation.
Ray - I read through what you sent as well. CURL looks to also work but since I am going to be doing several of these calls to construct a table it seems there wold be a lot more coding involved.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now