无法将响应解析为 JSON 错误 Symfony
Unable to Parse response as JSON error Symfony
我有一个发送获取请求然后尝试将响应解析为 JSON 的控制器。但是,通过浏览器请求它 returns 一个有效的 JSON object。我收到一个 500 错误,指出 'Exception: Unable to parse response as JSON' 并指向 Httpful
库中的 JsonHandler.php
。我查看了文件,这是因为 body 是空的。
我很困惑,因为请求 url 有效并且 returns 有效 JSON object。
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
});
我是不是漏掉了什么?
编辑:
尝试解析 body 并抛出异常的方法。
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 选项 curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true);
,它运行良好。
我有一个发送获取请求然后尝试将响应解析为 JSON 的控制器。但是,通过浏览器请求它 returns 一个有效的 JSON object。我收到一个 500 错误,指出 'Exception: Unable to parse response as JSON' 并指向 Httpful
库中的 JsonHandler.php
。我查看了文件,这是因为 body 是空的。
我很困惑,因为请求 url 有效并且 returns 有效 JSON object。
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
});
我是不是漏掉了什么?
编辑: 尝试解析 body 并抛出异常的方法。
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 选项 curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true);
,它运行良好。