PHP CURL 如何过滤json 结果?

PHP CURL How to filter json results?

需要过滤我的 curl json 请求。 我的问题是..我的输出列表总是完整的 JSON 列表 (Link 1-3)。 我只需要请求 Link 1 & Link 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"
        }
    ]
}

您似乎在尝试在 api 中执行此操作:/它甚至提供该功能吗?如果是这样,请询问他们,rtm 或给我们网站的真实 link 以便我们查看文档..

虽然如果不是我怀疑的那样,那么只需对结果使用 array filter

最好在从 API end 获取结果时过滤掉结果,但是如果无法在 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);
?>

演示: https://3v4l.org/BNotG