无法将响应解析为 JSON 错误 Symfony



我有一个控制器,它发送一个get请求,然后尝试将响应解析为JSON。但是,通过浏览器请求它会返回有效的 JSON 对象。我收到一个 500 错误,显示"异常:无法将响应解析为 JSON",并指向Httpful库中的JsonHandler.php。我浏览了文件,这是因为正文是空的。我很困惑,因为请求 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 HttpfulResponse $APIResponse */
        $APIResponse = HttpfulRequest::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 EXTRu00caME 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":"FRu00c9QUENCE 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

问题出在函数send Request.php 中。该方法使用 curl,并在curl_exec后检查响应后返回一个带有空正文的 301 代码。我所要做的就是设置一个新的卷曲选项curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true);它工作正常。

相关内容

  • 没有找到相关文章