tags:

views:

77

answers:

2

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")
    )
 )));
+2  A: 

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 />';
}
Silver Light
@djairo we can't know, it's up to you what you'll do with the data isn't it? Or is `$result["ROWS"]` not what you want?
Pekka
I've updated answer with example
Silver Light
Hello, we tried it but it prints an 'a' if i do a > var_dump($result['ROWS']); it give me > string(1) "a"
x4tje
A: 

/edit

we got this sorted in http://stackoverflow.com/questions/2552556/how-to-get-key-value-of-array-with-curl-php

x4tje
need some help !
djairo