Solved

PHP Retrieve values from external page

Posted on 2014-12-09
11
65 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
Comment Utility
<?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
Comment Utility
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
Comment Utility
Try it again, the site was not responding for me a few moments ago.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:lvmllc
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 look for a specific file type in a local or remote server directory using PHP.

763 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

9 Experts available now in Live!

Get 1:1 Help Now