【发布时间】:2015-10-30 01:05:46
【问题描述】:
我正在尝试合并来自 2 个 API 调用的数据。第一次调用获得了我需要的大部分数据,第二次调用使用第一次调用中的 item(bill_id) 作为参数,以便为第二次 API 调用获取数据。我只需要第二次调用中的一项,因此我尝试将其添加到第一个数组中,然后将该数组作为 json 传递给客户端。
这是 webmethod 控制器..
//AJAX calls
function getUpcomingBills ($params = null) {
$upcomingBills = $this->CongressAPI->get('upcoming_bills', $params);
$upcomingBills = json_decode($upcomingBills, true);
foreach($upcomingBills as $bill) {
$p = ['bill_id' => $bill['bill_id']];
$title = $this->CongressAPI->get('bills', $p);
$bill['title'] = $title[17];
}
echo json_encode($upcomingBills);
}
还有模特……
public function get($method = null, $params = null) {
$key = "...";
$url = "http://congress.api.sunlightfoundation.com/";
$params['apikey'] = $key;
$ret = $this->curl->simple_get($url.$method, $params);
if (!empty($ret)) {
$ret = json_decode($ret, true);
$ret = json_encode($ret["results"]);
} else {
$ret = '';
}
return $ret;
}
这将返回一个没有错误的 JSON 对象,但它没有将“标题”附加到第一个对象。
另外,当我尝试使用第二次调用的关联数组的标题(而不是 $title[17])时,我得到了错误:
消息:非法字符串偏移 'short_title'
第二次 API 调用返回以下内容:(尝试获取 'short_title')
{
"results": [
{
"bill_id": "s754-114",
"bill_type": "s",
"chamber": "senate",
"committee_ids": [
"SLIN"
],
"congress": 114,
"cosponsors_count": 0,
"enacted_as": null,
"history": {
"active": true,
"active_at": "2015-04-15",
"awaiting_signature": false,
"enacted": false,
"vetoed": false
},
"introduced_on": "2015-03-17",
"last_action_at": "2015-08-05",
"last_version": {
"version_code": "pcs",
"issued_on": "2015-03-17",
"version_name": "Placed on Calendar Senate",
"bill_version_id": "s754-114-pcs",
"urls": {
"html": "http://www.gpo.gov/fdsys/pkg/BILLS-114s754pcs/html/BILLS-114s754pcs.htm",
"pdf": "http://www.gpo.gov/fdsys/pkg/BILLS-114s754pcs/pdf/BILLS-114s754pcs.pdf",
"xml": "http://www.gpo.gov/fdsys/pkg/BILLS-114s754pcs/xml/BILLS-114s754pcs.xml"
},
"pages": 54
},
"last_version_on": "2015-03-17",
"last_vote_at": null,
"number": 754,
"official_title": "An original bill to improve cybersecurity in the United States through enhanced sharing of information about cybersecurity threats, and for other purposes.",
"popular_title": null,
"related_bill_ids": [
"hr1560-114",
"hr1731-114"
],
"short_title": "Cybersecurity Information Sharing Act of 2015",
"sponsor": {
"first_name": "Richard",
"last_name": "Burr",
"middle_name": "M.",
"name_suffix": null,
"nickname": null,
"title": "Sen"
},
"sponsor_id": "B001135",
"urls": {
"congress": "http://beta.congress.gov/bill/114th/senate-bill/754",
"govtrack": "https://www.govtrack.us/congress/bills/114/s754",
"opencongress": "https://www.opencongress.org/bill/s754-114"
},
"withdrawn_cosponsors_count": 0
}
],
"count": 1,
"page": {
"count": 1,
"per_page": 20,
"page": 1
}
}
如果有任何不清楚或需要更多信息,请告诉我。
【问题讨论】:
-
我觉得你应该看看这个问题的答案:stackoverflow.com/questions/15472033/…
-
感谢您的参考,它帮助我弄清楚了
标签: php arrays ajax json codeigniter