【问题标题】:How would i get the JSON data from a response like this in codeigniter我如何从 codeigniter 中这样的响应中获取 JSON 数据
【发布时间】:2015-01-02 23:30:03
【问题描述】:

我已经创建了一个查询 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" }

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

【问题讨论】:

    标签: php json codeigniter


    【解决方案1】:

    我将如何处理这类数据?

    您需要区分响应的标头和正文。比如:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2019-12-31
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多