我将如何在代码点火器中从这样的响应中获取 JSON 数据


How would i get the JSON data from a response like this in codeigniter

我创建了一个函数来查询 API,如下所示:

public function get_item($item_id = NULL)
    {
        $client = new Client();
        $url_string = $this->base_api."/item/".$item_id.$this->after_item;
        //https://hacker-news.firebaseio.com/v0//item/8705967.json?print=pretty
        $response = $client->get($url_string);
        return $response;
    }

以下是我收到的回复:

HTTP/1.1 200 OK 
Content-Length: 385 
Strict-Transport-Security: max-age=31556926; includeSubDomains; preload 
Content-Type: application/json; charset=utf-8 
Cache-Control: no-cache 
{ "by" : "theuser", "id" : 8705967, "parent" : 8705928, "text" : "I've done them all. Here's my writeup on the pros/cons of each: http://awebsite.com/the-definitive-guide-to-pro...", "time" : 1417804054, "type" : "comment" }

我将如何处理此类数据?能够将其放在数组中并对其进行操作。

我将如何处理此类数据?

您需要区分标头和响应正文。像这样:

$data = json_decode(end(explode("'r'n'r'n", $response, 2)), true);

但也许您希望您的 Client::get 函数只返回响应的正文。