Solved

PHP Retrieve values from external page

Posted on 2014-12-09
11
79 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

696 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