0

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
  • 1
    What's the problem you are having? Commented Mar 24, 2010 at 12:49
  • 1
    Clarify your question, what are you asking here? Commented Dec 7, 2012 at 6:36

1 Answer 1

2

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 />';
}
Sign up to request clarification or add additional context in comments.

2 Comments

@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?
Hello, we tried it but it prints an 'a' if i do a > var_dump($result['ROWS']); it give me > string(1) "a"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.