Solved

PHP Retrieve values from external page

Posted on 2014-12-09
11
76 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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
 

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 110

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

730 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