需要过滤我的curl json请求。 我的问题是..我的输出列表始终是完整的JSON列表(链接1-3)。 我只需要请求链接1和链接3。
请检查我的过滤器示例
$responseData = apiRequest("link/list", array('id' => json_encode(array('1001', '1003'))));
此过滤器不适用于我。我该如何解决我的问题? 我为每一个小贴士感到高兴
非常感谢
Api请求:
function apiRequest($command, $requestData = array()) {
$apiKey = "";
$headers = array(
'Authorization: APIKEY '.$apiKey
);
if (!is_array($requestData)) {
$requestData=array();
}
$requestData['apiKey'] = $apiKey;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_URL => 'https://api.example.com/'.$command,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $requestData)
);
if (($responseData = curl_exec($curl))===false) {
curl_close($curl);
/* echo "cURL error: ".curl_error($curl); */
return null;
}
return json_decode($responseData, true);
}
$responseData = apiRequest("link/list", array('id' => json_encode(array('1001', '1003'))));
Json列表:
{
"count": 3,
"links": [
{
"id": 1001,
"name": "Link 1",
"title": "Link Title",
"head": "Links",
"pic": "https://image.com/pic.jpg",
"views": "10,000+",
"country": "US"
}
{
"id": 1002,
"name": "Link 2",
"title": "Link Title 2",
"head": "Links",
"pic": "https://image.com/pic.jpg",
"views": "10,000+",
"country": "US"
}
{
"id": 1003,
"name": "Link 3",
"title": "Link Title 3",
"head": "Links",
"pic": "https://image.com/pic.jpg",
"views": "10,000+",
"country": "US"
}
]
}
答案 0 :(得分:0)
您似乎在api中尝试这样做:/它是否提供了该功能?如果是这样,请问他们,rtm或给我们该网站的真实链接,以便我们检查文档。
尽管我不怀疑,但是只需在结果上使用array filter。
答案 1 :(得分:0)
最好在从API端获取结果时过滤掉结果,但是如果无法在API上过滤掉结果,则可以使用php array_filter()
尝试这种方式,>
<?php
$api_response = '{"count":3,"links":[{"id":1001,"name":"Link 1","title":"Link Title","head":"Links","pic":"https://image.com/pic.jpg","views":"10,000+","country":"US"},{"id":1002,"name":"Link 2","title":"Link Title 2","head":"Links","pic":"https://image.com/pic.jpg","views":"10,000+","country":"US"},{"id":1003,"name":"Link 3","title":"Link Title 3","head":"Links","pic":"https://image.com/pic.jpg","views":"10,000+","country":"US"}]}';
$filter = [1001,1003];
$links = json_decode($api_response)->links;
$filtered = array_filter($links, function ($item) use ($filter) {
return in_array($item->id, $filter);
});
print_r($filtered);
?>