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

时间:2018-01-23 20:44:02

标签: php json rest symfony httpful

我有一个控制器发送一个get请求,然后尝试将响应解析为JSON。但是,通过浏览器请求它会返回一个有效的JSON对象。我收到500错误,上面写着'异常:无法解析响应为JSON'并指向JsonHandler.php库中的Httpful。我查看了文件,因为身体是空的。 我很困惑,因为请求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

1 个答案:

答案 0 :(得分:0)

问题出在函数Request.php的{​​{1}}中。该方法使用curl,并在send之后检查响应后,它返回了一个空体的301代码。我所要做的就是设置一个新的卷曲选项curl_exec并且它工作正常。

相关问题