我如何从codeigniter中的响应中获取JSON数据

时间:2015-01-02 23:30:03

标签: php json 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" }

我该如何处理这类数据?能够将它放在数组中并操纵它。

1 个答案:

答案 0 :(得分:0)

  

我该如何处理这类数据?

您需要将标题与响应正文区分开来。类似的东西:

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

但也许你想要你的Client :: get函数只返回响应的主体。