Benjamin Boufle Benjamin Boufle - 4 months ago 22
JSON Question

Json_decode return NULL with a valid JSON after receiving the data with curl

Hi there i'm triyng to decode my recived JSON with

json_decode()
but return me a NULL everytime.

foreach ($_data->response as $item){
$test[$item->plugin] = $item->new_version;
}
$compteur = 0;
$compteur2 = 0;
$values = array();
foreach ($apl as $p){
if ($_data != null){
if(isset($test[$p])) {

$values[$compteur] = array(
'id' => $compteur,
'plugin_name' => $plugins[$p]["Name"],
'plugin_version' => $plugins[$p]["Version"],
'plugin_up_to_date' => $test[$p],
);

}
else{

$values[$compteur] = array(
'id' => $compteur,
'plugin_name' => $plugins[$p]["Name"],
'plugin_version' => $plugins[$p]["Version"],
'plugin_up_to_date' => '0'
);

}

}
else{
array_push($values, array(
'id' => $compteur,
'plugin_name' => $plugins[$p]["Name"],
'plugin_version' => $plugins[$p]["Version"],
'plugin_up_to_date' => '0'
));
$compteur2++;
}
$compteur++;

}
$compteur = 0;
$compteur2 = 0;

array_push($values, array(
'Framework' => "WordPress",
'Version' => $wp_version,
));

$values = array_map("unserialize", array_unique(array_map("serialize", $values)));
$values = array_values($values);


/* Codage de la clé de cryptage sous 16 Bytes */
$enc = MCRYPT_RIJNDAEL_128;
$key = "mercijackymichel";
$mode = MCRYPT_MODE_CBC;
$text = "123";
$ivSize = 16;
$iv = str_repeat("\0", $ivSize);

/*returning the json*/

return $crypt = base64_encode(mcrypt_encrypt($enc, $key, json_encode($values), $mode, $iv));


This is my code that make my JSON

The JSON look like this :

[{"id":0,"plugin_name":"Akeeba Backup for WordPress","plugin_version":"1.9.0","plugin_up_to_date":"0"},{"id":1,"plugin_name":"Contact Form 7","plugin_version":"4.4.2","plugin_up_to_date":"0"},{"id":2,"plugin_name":"Cornerstone","plugin_version":"1.2.4","plugin_up_to_date":"1.3.0"},{"id":3,"plugin_name":"Duplicate Page","plugin_version":"1.4","plugin_up_to_date":"0"},{"id":4,"plugin_name":"Slider Revolution","plugin_version":"5.2.5.3","plugin_up_to_date":"5.2.6"},{"id":5,"plugin_name":"WordPress Importer","plugin_version":"0.6.1","plugin_up_to_date":"0"},{"id":6,"plugin_name":"Yoast SEO","plugin_version":"3.4","plugin_up_to_date":"3.4.1"},{"id":7,"plugin_name":"WP Migrate DB","plugin_version":"0.8","plugin_up_to_date":"0"},{"id":8,"plugin_name":"X – Google Analytics","plugin_version":"1.0.0","plugin_up_to_date":"0"},{"Framework":"WordPress","Version":"4.5.3"}]


And there is the code that call my php script :

$curlSession = curl_init();

echo '<br>'.$url.'<br><br>';

$ua = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2';
curl_setopt($curlSession, CURLOPT_URL, $url.'backup/getModul.php');
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curlSession, CURLOPT_USERAGENT, $ua);

$result =base64_decode( curl_exec($curlSession));

$httpCode = curl_getinfo($curlSession, CURLINFO_HTTP_CODE);

if ($httpCode == 401){
echo 'Problème de connexion check .htacces';
}else{


$decrypt = mcrypt_decrypt($enc, $key, $result, $mode, $iv);
var_dump($decrypt);
var_dump(json_decode($decrypt));
var_dump(json_last_error());
var_dump(json_last_error_msg());

}

curl_close($curlSession);


But this return me a NULL everytime, i alredy try to call
stripslashes()
on it, to rencode it in UTF-8 but nothing seems to work

Control character error, possibly incorrectly encoded


This is the error i get by calling
json_last_error()


If you have any idea .. Would be great !

Answer

Your encryption/decryption algorithm is adding padding to conform to the block-size. You should remove null-characters from the end, for example:

rtrim($decrypted, "\0");