php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53963 Failed to decode, yet json_last_error() is JSON_ERROR_NONE
Submitted: 2011-02-08 20:23 UTC Modified: 2011-02-09 09:15 UTC
From: hm2k@php.net Assigned: scottmac
Status: Closed Package: JSON related
PHP Version: 5.3.5 OS:
Private report: No CVE-ID:
 [2011-02-08 20:23 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-09 08:00 UTC] aharvey@php.net
-Status: Open +Status: Verified
 [2011-02-09 08:00 UTC] aharvey@php.net
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.
 [2011-02-09 08:35 UTC] scottmac@php.net
-Status: Verified +Status: Assigned -Assigned To: +Assigned To: scottmac
 [2011-02-09 09:05 UTC] scottmac@php.net
Automatic comment from SVN on behalf of scottmac
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=308155
Log: Fix Bug #53963, error code isn't always set in certain error cases.
 [2011-02-09 09:15 UTC] scottmac@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 13:02:15 2014 UTC