如何解码仅以JSON字符串作为响应的JSON响应

时间:2018-10-20 18:25:17

标签: php json api curl

我正在将cURL请求发送到返回此响应的API:

string(826) "HTTP/1.1 200
Server: openresty/1.13.6.2
Date: Sat, 20 Oct 2018 18:21:54 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 562
Connection: keep-alive
Expires: Sat, 20 Oct 2018 21:20:26 UTC
Cache-Control: max-age=10800
X-Cache-Status: HIT

{
    "SearchResponse": {
        "Matches": {
            "SearchMatch": [
                {
                    "Id": "43000173601",
                    "Lat": 52.4770035488100035,
                    "Lon": -1.71806745583999998,
                    "Name": "Yorkminster Drive",
                    "Modes": {
                        "Mode": "BUS_OR_COACH"
                    },
                    "TopMostParentId": "430G01736"
                },
                {
                    "Id": "43000173602",
                    "Lat": 52.4776389831399968,
                    "Lon": -1.71687080637,
                    "Name": "Yorkminster Drive",
                    "Modes": {
                        "Mode": "BUS_OR_COACH"
                    },
                    "TopMostParentId": "430G01736"
                }
            ]
        },
        "Query": "yorkminster",
        "Total": 2
    }
}

我想json_decode()进行JSON响应,但是不知道如何获取JSON部分并忽略烦人的顶部。我曾尝试使用explode(),但将其分解成超出我的头脑能力的片段,任何想法都将有所帮助。

这是我的cURL请求:

curl_init('api url');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$response = curl_exec($ch);
curl_close($ch);

0 个答案:

没有答案