0

I have made a simple api end point using Kimono for pulling Arkansas Waterfowl Reports and their respective post dates.

I am given the below api url from Kimono:

curl --include --request GET "http://www.kimonolabs.com/api/e45oypq8?apikey=XXXXX"

Because I am not familiar with how to pull data using cURL, I went to the web and read multiple articles, tutorials on pulling data from an api using cURL. I feel that there is about 1 million ways to do this. I have spent too much time banging head on desk. This is what I came up with:

<!DOCTYPE html>
<html>
<body>
  <?php
    $json_string = file_get_contents("http://www.kimonolabs.com/api/e45oypq8?apikey=XXX");
    $parsed_json = json_decode($json_string);
    $title = $parsed_json->{'results'}->{'collection1'}->{'title'};
    $posted = $parsed_json->{'results'}->{'collection1'}->{'posted'};
    echo "${title} \n ${posted}\n\n";
  ?>
</body>
</html>

The api endpoint spits out the following (truncated for length of question):

{
  name: "agfc",
  lastrunstatus: "success",
  lastsuccess: "Fri Jan 17 2014 06:39:54 GMT+0000 (UTC)",
  nextrun: "Sat Jan 18 2014 06:39:54 GMT+0000 (UTC)",
  frequency: "daily",
  newdata: true,
  results: {
      collection1: [
          {
            title: {
            text: "January 8, 2014 Weekly Waterfowl Report",
            href: "http://e2.ma/message/zgkue/nnlu0d"
            },
            posted: "1/8/2014"
            }
          ]
}

I simply want to pull all of the data from the api endpoint and 'echo' '$title' and '$posted' linking to the attributed url('href') of each of the data points.

I am sure there is an easy way to do it. I am missing something. Thanks for your help.

1
  • I removed your API key from the question, you should NEVER include such keys when you publish code. Commented Jan 17, 2014 at 10:12

3 Answers 3

2

'collection1' is an array.

$title = $parsed_json->{'results'}->{'collection1'}[0]->{'title'}->text;

If collection1 holds more than 1 element you have to loop through them.

foreach ($parsed_json->{'results'}->{'collection1'} as $item) {
  $title = $item->title->text;
  $posted = $item->posted;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Here is what I now have and it works as intended. Thank you! <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.kimonolabs.com/api/e45oypq8?apikey=XXXXX"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $parsed_json = curl_exec($ch); $parsed_json = json_decode($parsed_json); foreach ($parsed_json->{'results'}->{'collection1'} as $item) { $title = $item->title->text; $link_address = $item->title->href; $posted = $item->posted; echo "<a href='$link_address'> $title </a>" . '<br>'; echo $posted . '<br><hr><br>'; } ?>
0

Just try:

$json_string = file_get_contents("http://www.kimonolabs.com/api/e45oypq8?apikey=YOUR_API_KEY");
    //json string to array
$parsed_arr = json_decode($json_string,true);


$collection1=$parsed_arr['results']['collection1'];
for($i=0;$i<count($collection1);$i++)
{
    echo $collection1[$i]['title']['text']."--".$collection1[$i]['posted']."<br/>"; 
}

Comments

0

one way using curl

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.kimonolabs.com/api/e45oypq8?apikey=xxxx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$parsed_json = curl_exec($ch);
$parsed_json = json_decode($parsed_json);

foreach($parsed_json->results->collection1 as $collection){
    echo $collection->title->text . '<br>';
    echo $collection->title->href . '<br>';
    echo $collection->posted . '<br><br>';
}

curl_close($ch);
?>

another that you did

<?php
$json_string = file_get_contents("http://www.kimonolabs.com/api/e45oypq8?apikey=XXX");
$parsed_json = json_decode($json_string);
//var_dump($parsed_json->results->collection1);

foreach($parsed_json->results->collection1 as $collection){
    echo $collection->title->text . '<br>';
    echo $collection->title->href . '<br>';
    echo $collection->posted . '<br><br>';
}
?>

Comments

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.