0

I have this text in my $result

{"meta":{"code":400,"message":"Bad Request"},"error":"userId is required.","extras":null}

But when I do

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

It gives me null. I have validated this text everywhere and it says that it is a valid json.

EDIT

This is my code

<?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);
?>
ADyson
  • 57,178
  • 14
  • 51
  • 63
Ali Zia
  • 3,825
  • 5
  • 29
  • 77

3 Answers3

2

I have solved this issue please check this below code

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

?>

I am sure it's work properly please check

Curl sent json response .it's display proper json but it's have unwanted characters. we have remove unwanted characters binary level.Then pass to json_decode function

Happy Programming

Thanks, AS

Abhijit
  • 931
  • 1
  • 9
  • 21
  • @Ali Zia : Have you checked my answer ? – Abhijit Nov 13 '18 at 12:28
  • would you like to explain to us exactly what you've done in the code to solve it, in case it's not obvious what you have changed, and more importantly _why_ you changed it. – ADyson Nov 13 '18 at 12:42
  • @ADyson : Curl sent json response .it's display proper json but it's have unwanted characters. we have remove unwanted characters binary level.Then pass to json_decode function – Abhijit Nov 13 '18 at 12:48
  • 1
    please add the explanation to the answer itself - thanks. – ADyson Nov 13 '18 at 12:49
-1

Hi I found that on 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);
?>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
jawbonewalk
  • 162
  • 2
  • 9
-1

just you need to add the trim before decoding your code.You will get the result as it is getting some extra data