Reputation: 29
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.
Upvotes: 0
Views: 25411
Reputation: 2597
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>';
}
?>
Upvotes: 0
Reputation: 5689
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/>";
}
Upvotes: 0
Reputation: 17797
'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;
}
Upvotes: 2