-1

我有一个控制器,它发送一个 get 请求,然后尝试将响应解析为 JSON。但是,通过浏览器请求它会返回一个有效的 JSON 对象。我收到一个 500 错误,上面写着“异常:无法将响应解析为 JSON”并指向库中JsonHandler.phpHttpful我查看了文件,这是因为正文是空的。我很困惑,因为请求 url 是有效的并返回一个有效的 JSON 对象。

Controller.php

        Authorization::checkUser();
        $request = $this->request;

        $callback = $request->query->get('callback');
        $url = 'http://example.com/api/get_recent_posts/?' . http_build_query(
            [
                'count'     => 20,
                'post_type' => 'schedule_show',
                'page'      => (int) $request->query->get('page', 1),
                'order'     => 'ASC',
                'orderby'   => 'title',
                'include'   => 'id,title',
                '_'         => (int) $request->query->get('_', time()),
            ]
        );

        /** @var \Httpful\Response $APIResponse */

        $APIResponse = \Httpful\Request::get($url, 'application/json')->send();
        $data = $APIResponse->body;
        $response = new JsonResponse($data);

        $response->setCallback($callback);

        return $response;

JSON响应:

     {  
   "status":"ok",
   "count":20,
   "count_total":70,
   "pages":4,
   "posts":[  
      {  
         "id":2473,
         "title":"ACOUSTIC ROOTS"
      },
      {  
         "id":2531,
         "title":"AFRIKA REVISITED"
      },
      {  
         "id":2542,
         "title":"AMANECER RANCHERO"
      },
      {  
         "id":2551,
         "title":"APNIVANI"
      },
      {  
         "id":2504,
         "title":"APT 613 LIVE"
      },
      {  
         "id":6229,
         "title":"ATMOSPHERE"
      },
      {  
         "id":2532,
         "title":"BLACK ON BLACK"
      },
      {  
         "id":2550,
         "title":"BOUYON RASIN"
      },
      {  
         "id":2462,
         "title":"CAN-ROCK"
      },
      {  
         "id":2534,
         "title":"CARIBBEAN FLAVOUR"
      },
      {  
         "id":5288,
         "title":"CHUO TOP 30"
      },
      {  
         "id":6060,
         "title":"CINEMASCOPE"
      },
      {  
         "id":2930,
         "title":"CITY SLANG"
      },
      {  
         "id":2524,
         "title":"CYPHER"
      },
      {  
         "id":2484,
         "title":"D’UN EXTR\u00caME \u00c0 L’AUTRE"
      },
      {  
         "id":2478,
         "title":"DEMOCKERY’S DEMISE"
      },
      {  
         "id":2438,
         "title":"DEMOCRACY NOW!"
      },
      {  
         "id":2546,
         "title":"ETHIOPIAN SHOW"
      },
      {  
         "id":5930,
         "title":"FREESTYLE"
      },
      {  
         "id":6247,
         "title":"FR\u00c9QUENCE ANTILLAISE"
      }
   ]
}

AJAX 请求:

$.ajax('/shows/select/load', {
  data: {
    count: 20,
    post_type: 'schedule_show',
    page: this.page_current,
    order: 'ASC',
    orderby: 'title',
    include: 'id,title'
  },
  dataType: 'jsonp',
  context: this,
  success: this.loadShows
});

我错过了什么吗?

编辑:尝试解析主体并引发异常的方法。

JsonHandler.php:

public function parse($body)
    {
        $body = $this->stripBom($body);
        if (empty($body))
            return null;
        $parsed = json_decode($body, $this->decode_as_array);
        if (is_null($parsed) && 'null' !== strtolower($body))
            throw new \Exception("Unable to parse response as JSON");
        return $parsed;
    }

出于调试目的,我添加了这些 var_dump 语句:

public function parse($body)
    {
        var_dump($body);
        $body = $this->stripBom($body);
        var_dump($body);
        if (empty($body))
            return null;
        var_dump($this->decode_as_array);
        $parsed = json_decode($body, $this->decode_as_array);
        var_dump($parsed);
        if (is_null($parsed) && 'null' !== strtolower($body))
            throw new \Exception("Unable to parse response as JSON");
        return $parsed;
    }

这返回:

string(1) " " 
string(1) " " 
bool(false) 
NULL
4

1 回答 1

0

问题出Request.php在函数中sendcurl_exec该方法使用 curl 并在返回带有空正文的 301 代码后检查响应。我所要做的就是设置一个新的 curl 选项curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true);而且效果很好。

于 2018-01-24T17:47:28.723 回答