使用 cURL 在 PHP 中获取 api 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21182946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using cURL to GET api data within PHP
提问by joshclemence
I have made a simple api end point using Kimono for pulling Arkansas Waterfowl Reports and their respective post dates.
我使用 Kimono 制作了一个简单的 api 端点,用于拉取阿肯色州水禽报告及其各自的发布日期。
I am given the below api url from Kimono:
我从 Kimono 获得了以下 api 网址:
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:
因为我不熟悉如何使用 cURL 拉取数据,所以我去网上阅读了多篇关于使用 cURL 从 api 拉取数据的文章、教程。我觉得大约有 100 万种方法可以做到这一点。我花了太多时间在桌子上撞头。这就是我想出的:
<!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):
api 端点吐出以下内容(因问题长度而被截断):
{
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.
我只是想从 api 端点和链接到每个数据点的属性 url('href') 的 'echo' '$title' 和 '$posted' 中提取所有数据。
I am sure there is an easy way to do it. I am missing something. Thanks for your help.
我相信有一种简单的方法可以做到。我错过了一些东西。谢谢你的帮助。
回答by Gerald Schneider
'collection1' is an array.
'collection1' 是一个数组。
$title = $parsed_json->{'results'}->{'collection1'}[0]->{'title'}->text;
If collection1 holds more than 1 element you have to loop through them.
如果 collection1 包含 1 个以上的元素,则必须遍历它们。
foreach ($parsed_json->{'results'}->{'collection1'} as $item) {
$title = $item->title->text;
$posted = $item->posted;
}
回答by sas
one way using curl
使用 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>';
}
?>
回答by Jenson M John
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/>";
}

