Problem PHP with cURL
hi i have a problem with cURL and php. I use an API and i want to display only the results. Here is my php + curl script
<?php
$file = "http://api.openkvk.nl/php/".rawurlencode("SELECT * FROM kvk WHERE kvks='20147376'");
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
and the result what i get is this:
array(
array("RESULT" => array(
"TYPES"=>
array(
"int", "bigint", 开发者_如何学运维"varchar", "varchar", "varchar", "varchar", "varchar", "int", "int", "smallint", "smallint", "int"
),
"HEADER"=>
array("id", "kvk", "bedrijfsnaam", "adres", "postcode", "plaats", "type", "kvks", "sub", "bedrijfsnaam_size", "adres_size", "verhuisd"),
"ROWS"=>
array(
array("1008714", "20147376", "Stichting OpenGeo", "Spinellihof 44", "4463GP", "Goes", "Rechtspersoon", "20147376", NULL, "17", "14", "0")
)
)));
Your script outputs data, instead of returning it. Add this line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
And then assign curl_exec to some variable:
$result = curl_exec($ch);
It seems that API returns some META information, such as field types. If you don't need it, the just work with 'ROWS' element
print_r($result['ROWS']);
It will look like this:
array(
"1008714","20147376","Stichting OpenGeo","Spinellihof 44","4463GP","Goes","Rechtspersoon","20147376",NULL,"17","14","0"
)
You can iterate through it:
for($i=0; $i<count($result['ROWS']); $i++)
{
echo $result['HEADER'][$i].': '.$result['ROWS'][$i].'<br />';
}
精彩评论