Solved

PHP Retrieve values from external page

Posted on 2014-12-09
11
72 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 109

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
curl SSL certificate unable to get local issuer certificate 15 72
AWS EC2 & RDS Instance 5 36
Attach to file (img) a unique id 8 27
send post values 7 26
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 count occurrences of each item in an array.

832 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