Bug #53963 [Opn->Ver]: Failed to decode, yet json_last_error() is JSON_ERROR_NONE
| From: | aharvey@php.net | Date: | Wed, 09 Feb 2011 07:00:50 +0000 |
| Subject: | Bug #53963 [Opn->Ver]: Failed to decode, yet json_last_error() is JSON_ERROR_NONE | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-158621@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=53963&edit=1
ID: 53963
Updated by: aharvey@php.net
Reported by: hm2k@php.net
Summary: Failed to decode, yet json_last_error() is
JSON_ERROR_NONE
-Status: Open
+Status: Verified
Type: Bug
Package: JSON related
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
Here's a much simpler test case based on the munged JSON from
atsearch.autotrader.co.uk:
<?php
$input = '{"searchform":null}, null,
{"searchresults":"foo"}';
var_dump(json_decode($input));
var_dump(json_last_error());
?>
Obviously json_last_error() should return something other than 0,
presumably JSON_ERROR_SYNTAX (4), since it's not syntactically
valid JSON.
Previous Comments:
------------------------------------------------------------------------
[2011-02-08 20:23:48] hm2k@php.net
Description:
------------
I'm trying the json_decode() function on real world strings, not just
from strings
produced using json_encode().
I have included the test case from bug #52262 as well to show that this
issue is
different.
Thanks.
Test script:
---------------
<pre>
<?php
/* Settings */
/* from http://dealerservices.autotrader.co.uk/14184/cars.htm
*/
$urls[]='http://atsearch.autotrader.co.uk/js/uvl/InlineServlet.js?did=14184&configfile=http://dealerservices.autotrader.co.uk/dealers/14184/14184_config.xml&csslocation=http://dealerservices.autotrader.co.uk/dealers/14184/14184&dropdowntype=uvl&partner=TMG&postcode=me86ad&miles=1500&sort=5&action=searchresults';
/* from http://api.jquery.com/jQuery.getJSON/ */
$urls[]='http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=jQuery15094986786483787_1297190250272&tags=cat&tagmode=any&format=json&_=1297190250280';
/* from
http://code.google.com/apis/base/samples/javascript/tutorial.html
*/
$urls[]='http://www.google.com/base/feeds/attributes/-/vehicles?max-values=30&bq=[target%20country:GB]&alt=json-in-script&callback=showMake';
/* from http://code.google.com/apis/gdata/docs/json.html
*/
$urls[]='http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json';
/* from http://code.google.com/apis/gdata/docs/json.html
*/
$urls[]='http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json-in-script&callback=myFunction';
/* from http://bugs.php.net/bug.php?id=52262 */
$urls[]='http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0001/?gameid=440';
/* from http://php.net/manual/en/function.json-last-error.php
*/
$json_errors=array( -1 => 'An unknown error occured',
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been
exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly
incorrectly encoded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly
incorrectly encoded');
/* main loop */
foreach($urls as $url) {
echo PHP_EOL;
//call the json
$json=file_get_contents($url);
echo 'Decoding... ' . $url . PHP_EOL;
//convert from jsonp to json
$json=preg_replace('/.+?({.+}).+/','$1',$json);
//get a result
$result=json_decode($json);
if ($result) {
echo 'Decoded.';
} else {
echo 'Failed to decode.';
}
echo PHP_EOL;
$err=json_last_error();
if ($err != JSON_ERROR_NONE) {
echo 'Error: ';
echo isset($json_errors[$err])?$json_errors[$err]:$json_errors[-1];
} else {
echo 'No error.';
}
echo PHP_EOL;
}
//eof
Expected result:
----------------
The first result should either:
* Decode successfully and have no error (preferable)
Or
* Fail to decode and have an error which explains why
Actual result:
--------------
Decoding... http://atsearch.autotrader.co.uk/js/uvl/InlineServlet.js?
did=14184&configfile=http://dealerservices.autotrader.co.uk/dealers/14184/14184_
config.xml&csslocation=http://dealerservices.autotrader.co.uk/dealers/14184/1418
4&dropdowntype=uvl&partner=TMG&postcode=me86ad&miles=1500&sort=5&action=searchre
sults
Failed to decode.
No error.
Decoding... http://api.flickr.com/services/feeds/photos_public.gne?
jsoncallback=jQuery15094986786483787_1297190250272&tags=cat&tagmode=any&format=j
son&_=1297190250280
Failed to decode.
Error: Syntax error
Decoding... http://www.google.com/base/feeds/attributes/-/vehicles?max-
values=30&bq=[target%20country:GB]&alt=json-in-script&callback=showMake
Decoded.
No error.
Decoding... http://www.google.com/calendar/feeds/developer-
calendar@google.com/public/full?alt=json
Decoded.
No error.
Decoding... http://www.google.com/calendar/feeds/developer-
calendar@google.com/public/full?alt=json-in-script&callback=myFunction
Decoded.
No error.
Decoding...
http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForAp
p/v0001/?gameid=440
Decoded.
No error.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=53963&edit=1