我的$ result中有此文字
{“ meta”:{“ code”:400,“ message”:“ Bad Request”},“ error”:“ userId is required。”,“ 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);
?>
答案 0 :(得分:3)
我已经解决了这个问题,请检查下面的代码
<?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);
?>
我确定它可以正常工作,请检查
Curl发送了json响应。它显示正确的json,但其中包含不需要的字符。我们已经删除了不需要的字符二进制级别。然后传递给json_decode函数
快乐编程
谢谢, AS
答案 1 :(得分:-1)
嗨,我发现这是在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);
?>
答案 2 :(得分:-1)
只是您需要在解码代码之前添加修饰,您将得到结果,因为它正在获取一些额外的数据