收到curl

时间:2016-08-03 13:24:58

标签: php json curl

嗨,我正在尝试用json_decode()解码我收到的JSON,但每次都返回NULL。

     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));

这是我制作JSON的代码

JSON看起来像这样:

    [{"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"}]

还有代码调用我的php脚本:

    $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);

但是这次每次都给我一个NULL,我试着在它上面调用stripslashes(),用UTF-8重新编码,但似乎什么都没有用

    Control character error, possibly incorrectly encoded

这是我通过调用json_last_error()

得到的错误

如果您有任何想法......会很棒!

1 个答案:

答案 0 :(得分:0)

您的加密/解密算法正在添加填充以符合块大小。您应该从末尾删除空字符,例如:

rtrim($decrypted, "\0");