代码之家  ›  专栏  ›  技术社区  ›  Ali Zia

json_decode在有效的json上为空

  •  0
  • Ali Zia  · 技术社区  · 7 年前

    我的$result里有这条短信

    {“meta”:{“code”:400,“message”:“Bad Request”},“error”:“需要用户id。”,“extras”:null}

    $json_result = json_decode($result, true);
    print_r($json_result);
    

    无效的 . 我已经在任何地方验证了这个文本,它说它是一个有效的json。

    编辑

    <?php
    
    $data = "&userId=";
    $data_string = $data;
    $url = 'http://apptellect.cloudapp.net/binance/api/v1/get_user_assets/';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    echo $result = curl_exec($ch);
    echo '<hr>';
    curl_close($ch);
    $json_result = json_decode($result, true);
    echo json_last_error_msg();
    echo '<hr>';
    //$json_result = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $result), true );
    print_r($json_result);
    ?>
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Abhijit    7 年前

    我已经解决了这个问题,请检查下面的代码

    <?php
    // Your code here!
    $data = "&userId=";
    $data_string = $data;
    $url = 'http://apptellect.cloudapp.net/binance/api/v1/get_user_assets/';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    echo $result = curl_exec($ch);
    echo '<hr>';
    curl_close($ch);
    
    // This will remove unwanted characters.
    // Check http://www.php.net/chr for details
    for ($i = 0; $i <= 31; ++$i) { 
        $result = str_replace(chr($i), "", $result); 
    }
    $result = str_replace(chr(127), "", $result);
    
    // This is the most common part
    // Some file begins with 'efbbbf' to mark the beginning of the file. (binary level)
    // here we detect it and we remove it, basically it's the first 3 characters 
    if (0 === strpos(bin2hex($result), 'efbbbf')) {
       $result = substr($result, 3);
    }
    
    $json_result =    json_decode($result, true);
    
    echo json_last_error_msg();
    echo '<hr>';
    print_r($json_result);
    
    ?>
    

    快乐编程

    谢谢,

        2
  •  -1
  •   Death-is-the-real-truth    7 年前

    嗨,我在stackoverflow上发现: here

    <?php
    
    function removeBOM($data) {
        if (0 === strpos(bin2hex($data), 'efbbbf')) {
           return substr($data, 3);
        }
        return $data;
    }
    
    $data = "&userId=";
    $data_string = $data;
    $url = 'http://apptellect.cloudapp.net/binance/api/v1/get_user_assets/';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec($ch);
    $result = removeBOM($result);
    echo $result;
    
    curl_close($ch);
    $json_result = json_decode(trim($result), false);
    echo json_last_error_msg();
    //$json_result = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $result), true );
    print_r($json_result);
    ?>
    
        3
  •  -1
  •   akshika gupta    7 年前

    你只需要在解码你的密码。你会得到一些额外的数据