json_decode giving null on a valid json
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-x1Fx80-xFF]/', '', $result), true );
print_r($json_result);
?>
php json curl
|
show 6 more comments
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-x1Fx80-xFF]/', '', $result), true );
print_r($json_result);
?>
php json curl
What does json_last_error_msg tell you?
– Paul
Nov 13 '18 at 11:08
It says 4 but I don't understand why does it say 4
– Ali Zia
Nov 13 '18 at 11:09
4
Works for me on pretty much all PHP versions: 3v4l.org/KNUdP perhaps you have some non-printing characters in there?
– Nick
Nov 13 '18 at 11:09
No just this text
– Ali Zia
Nov 13 '18 at 11:10
4 = "JSON_ERROR_SYNTAX" - php.net/manual/en/function.json-last-error.php. Show us how you're setting$result
.
– Utkanos
Nov 13 '18 at 11:11
|
show 6 more comments
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-x1Fx80-xFF]/', '', $result), true );
print_r($json_result);
?>
php json curl
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-x1Fx80-xFF]/', '', $result), true );
print_r($json_result);
?>
php json curl
php json curl
edited Nov 13 '18 at 11:38
ADyson
23.6k112445
23.6k112445
asked Nov 13 '18 at 11:07
Ali ZiaAli Zia
2,3491841
2,3491841
What does json_last_error_msg tell you?
– Paul
Nov 13 '18 at 11:08
It says 4 but I don't understand why does it say 4
– Ali Zia
Nov 13 '18 at 11:09
4
Works for me on pretty much all PHP versions: 3v4l.org/KNUdP perhaps you have some non-printing characters in there?
– Nick
Nov 13 '18 at 11:09
No just this text
– Ali Zia
Nov 13 '18 at 11:10
4 = "JSON_ERROR_SYNTAX" - php.net/manual/en/function.json-last-error.php. Show us how you're setting$result
.
– Utkanos
Nov 13 '18 at 11:11
|
show 6 more comments
What does json_last_error_msg tell you?
– Paul
Nov 13 '18 at 11:08
It says 4 but I don't understand why does it say 4
– Ali Zia
Nov 13 '18 at 11:09
4
Works for me on pretty much all PHP versions: 3v4l.org/KNUdP perhaps you have some non-printing characters in there?
– Nick
Nov 13 '18 at 11:09
No just this text
– Ali Zia
Nov 13 '18 at 11:10
4 = "JSON_ERROR_SYNTAX" - php.net/manual/en/function.json-last-error.php. Show us how you're setting$result
.
– Utkanos
Nov 13 '18 at 11:11
What does json_last_error_msg tell you?
– Paul
Nov 13 '18 at 11:08
What does json_last_error_msg tell you?
– Paul
Nov 13 '18 at 11:08
It says 4 but I don't understand why does it say 4
– Ali Zia
Nov 13 '18 at 11:09
It says 4 but I don't understand why does it say 4
– Ali Zia
Nov 13 '18 at 11:09
4
4
Works for me on pretty much all PHP versions: 3v4l.org/KNUdP perhaps you have some non-printing characters in there?
– Nick
Nov 13 '18 at 11:09
Works for me on pretty much all PHP versions: 3v4l.org/KNUdP perhaps you have some non-printing characters in there?
– Nick
Nov 13 '18 at 11:09
No just this text
– Ali Zia
Nov 13 '18 at 11:10
No just this text
– Ali Zia
Nov 13 '18 at 11:10
4 = "JSON_ERROR_SYNTAX" - php.net/manual/en/function.json-last-error.php. Show us how you're setting
$result
.– Utkanos
Nov 13 '18 at 11:11
4 = "JSON_ERROR_SYNTAX" - php.net/manual/en/function.json-last-error.php. Show us how you're setting
$result
.– Utkanos
Nov 13 '18 at 11:11
|
show 6 more comments
3 Answers
3
active
oldest
votes
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
@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
add a comment |
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-x1Fx80-xFF]/', '', $result), true );
print_r($json_result);
?>
found what exactly? Can you explain your code please and how it solves the problem.
– ADyson
Nov 13 '18 at 11:38
Please also explain the code so that I can understand what removeBOM is doing
– Ali Zia
Nov 13 '18 at 11:39
jawbonewalk when you found a duplicate link then try to close the question with that link instead of re-answering the question with same code.
– Alive to Die
Nov 13 '18 at 11:57
add a comment |
just you need to add the trim before decoding your code.You will get the result as it is getting some extra data
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279669%2fjson-decode-giving-null-on-a-valid-json%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
@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
add a comment |
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
@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
add a comment |
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
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
edited Nov 13 '18 at 12:52
answered Nov 13 '18 at 12:16
AbhijitAbhijit
1,002715
1,002715
@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
add a comment |
@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
@Ali Zia : Have you checked my answer ?
– Abhijit
Nov 13 '18 at 12:28
@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
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
@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
1
please add the explanation to the answer itself - thanks.
– ADyson
Nov 13 '18 at 12:49
please add the explanation to the answer itself - thanks.
– ADyson
Nov 13 '18 at 12:49
add a comment |
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-x1Fx80-xFF]/', '', $result), true );
print_r($json_result);
?>
found what exactly? Can you explain your code please and how it solves the problem.
– ADyson
Nov 13 '18 at 11:38
Please also explain the code so that I can understand what removeBOM is doing
– Ali Zia
Nov 13 '18 at 11:39
jawbonewalk when you found a duplicate link then try to close the question with that link instead of re-answering the question with same code.
– Alive to Die
Nov 13 '18 at 11:57
add a comment |
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-x1Fx80-xFF]/', '', $result), true );
print_r($json_result);
?>
found what exactly? Can you explain your code please and how it solves the problem.
– ADyson
Nov 13 '18 at 11:38
Please also explain the code so that I can understand what removeBOM is doing
– Ali Zia
Nov 13 '18 at 11:39
jawbonewalk when you found a duplicate link then try to close the question with that link instead of re-answering the question with same code.
– Alive to Die
Nov 13 '18 at 11:57
add a comment |
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-x1Fx80-xFF]/', '', $result), true );
print_r($json_result);
?>
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-x1Fx80-xFF]/', '', $result), true );
print_r($json_result);
?>
edited Nov 13 '18 at 12:03
Alive to Die
55.8k82869
55.8k82869
answered Nov 13 '18 at 11:35
jawbonewalkjawbonewalk
16218
16218
found what exactly? Can you explain your code please and how it solves the problem.
– ADyson
Nov 13 '18 at 11:38
Please also explain the code so that I can understand what removeBOM is doing
– Ali Zia
Nov 13 '18 at 11:39
jawbonewalk when you found a duplicate link then try to close the question with that link instead of re-answering the question with same code.
– Alive to Die
Nov 13 '18 at 11:57
add a comment |
found what exactly? Can you explain your code please and how it solves the problem.
– ADyson
Nov 13 '18 at 11:38
Please also explain the code so that I can understand what removeBOM is doing
– Ali Zia
Nov 13 '18 at 11:39
jawbonewalk when you found a duplicate link then try to close the question with that link instead of re-answering the question with same code.
– Alive to Die
Nov 13 '18 at 11:57
found what exactly? Can you explain your code please and how it solves the problem.
– ADyson
Nov 13 '18 at 11:38
found what exactly? Can you explain your code please and how it solves the problem.
– ADyson
Nov 13 '18 at 11:38
Please also explain the code so that I can understand what removeBOM is doing
– Ali Zia
Nov 13 '18 at 11:39
Please also explain the code so that I can understand what removeBOM is doing
– Ali Zia
Nov 13 '18 at 11:39
jawbonewalk when you found a duplicate link then try to close the question with that link instead of re-answering the question with same code.
– Alive to Die
Nov 13 '18 at 11:57
jawbonewalk when you found a duplicate link then try to close the question with that link instead of re-answering the question with same code.
– Alive to Die
Nov 13 '18 at 11:57
add a comment |
just you need to add the trim before decoding your code.You will get the result as it is getting some extra data
add a comment |
just you need to add the trim before decoding your code.You will get the result as it is getting some extra data
add a comment |
just you need to add the trim before decoding your code.You will get the result as it is getting some extra data
just you need to add the trim before decoding your code.You will get the result as it is getting some extra data
answered Nov 13 '18 at 12:23
akshika guptaakshika gupta
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279669%2fjson-decode-giving-null-on-a-valid-json%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What does json_last_error_msg tell you?
– Paul
Nov 13 '18 at 11:08
It says 4 but I don't understand why does it say 4
– Ali Zia
Nov 13 '18 at 11:09
4
Works for me on pretty much all PHP versions: 3v4l.org/KNUdP perhaps you have some non-printing characters in there?
– Nick
Nov 13 '18 at 11:09
No just this text
– Ali Zia
Nov 13 '18 at 11:10
4 = "JSON_ERROR_SYNTAX" - php.net/manual/en/function.json-last-error.php. Show us how you're setting
$result
.– Utkanos
Nov 13 '18 at 11:11