json_error_ctrl_char error

Hi

i got this error 4(json_error_ctrl_char error) for json_Decode. when i put var_dump it return string as like


string(534) " {"data":"data","data":"data","data":"data","data":"data"}" 

how to decode this json data? help me to solve this problem . what kind of error is this

thanks
sona

The error means the array data was incorrectly encoded, could you please post the code you used to encode the array.

i have read jsondata from url , using curl


$file="someurl";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_REFERER,1);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);

when i run the passing url it display json data like below


{"data":"data","data":"data","data":"data","data":"data"}

if i put var_dump($result);

it show like


string(534) " {"data":"data","data":"data","data":"data","data":"data"}"

i view the page source of the php file to read contents of the curl
it display like


string(534) "
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
{"data":"data","data":"data","daa":"data","data":"data"}<br>

what problem in the code , is it code or json data

Is the JSON data coming from another file on your server or from a 3rd party server as there is nothing in your script above that is turning it into a string.

is problem with json data or my code?

The problem lies with how the JSON data is been proceeded and sent back through your cURL connection, until i get a look into the other side of the code its hard to know whats going on with the encoding process.

i have just read the data from that url, i m not posting anything

is there any problem in utf8 and UTF-8 meta tag .

this is the html file having json feed


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
{"data":"data","data":"data","daa":"data","data":"data"}<br>
</body>
</html>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

$file="htmlfileurll";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_REFERER,1);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
$data=json_decode($result);
</body>
</html>

is there problem on meta tag of both files?

output is NULL

what is the problem in the code

The problem isn’t with the content encoding aka UTF-8, the problem lies with the how the data is been sent as a response and in your examples above the response will always be a string because the JSON array is just plain text.

In order for JSON to work correctly a PHP array needs to be encoded and returned as an array not a string, see the example below…

request.php

<?php

$myArray = array('data' => 'hello', 'key' => 'bye');
$myArray = json_encode($myArray); // Ouput: {data: 'hello', key: 'bye'}
echo $myArray;

?>

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

$file = 'request.php';  
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $file);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
curl_setopt($ch, CURLOPT_REFERER,1);  
$result = curl_exec($ch);  
curl_close($ch);  
 
$data = json_decode($result);
var_dump($data); 

?>

</body>
</html>

The above example will work correctly because the JSON array will be returned as an array and not a string.

Hi
if i put this in php file like below i got output


<?php

$myArray = array('data' => 'hello', 'key' => 'bye');
$myArray = json_encode($myArray); // Ouput: {data: 'hello', key: 'bye'}
echo $myArray;

?>

instead of like below i included means it return null


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

$myArray = array('data' => 'hello', 'key' => 'bye');
$myArray = json_encode($myArray); // Ouput: {data: 'hello', key: 'bye'}
echo $myArray;

?>

</body>
</html>

what is the problem in my index.php

index.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

$file = 'request.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_REFERER,1);
$result = curl_exec($ch);
curl_close($ch);

$data = json_decode($result);
var_dump($data);

?>

</body>
</html>

The problem is you have HTML wrapping the request which gets returned with the array, typically when calling for PHP requests you NEVER have HTML blended in with the PHP like you do above because it will return everything as a string and not the correct parsing type that PHP needs to use the response correctly. In my example the request.php file contains no HTML which allows for a clean and working response.

Thank you for you help SgtLegend