0

With this code :

$url = 'https://www.xxxxxxx.com/api/v1/phone/?apikey=xxxxxxxxxx&id='.$id;

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json'
));
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($cURL);

curl_close($cURL);

    echo $result;    
    var_dump ($result);
    var_dump(json_decode($result, true));

I get that :

{"telephone":"0 811 228 338"}

string(32) "{"telephone":"X XXX XXX XXX"}"

NULL

I don't understand why json_decode doesn't work here?

Thanks for helping me

Community
  • 1
  • 1
Silver Dragon
  • 33
  • 1
  • 1
  • 5

2 Answers2

1

with the little info i got i believe its a json formating problem.

<?php

    $json = '{"foo-bar": 12345}';

    $obj = json_decode($json);
    print $obj->{'foo-bar'}; // 12345

?>

the json quotes are not valid http://php.net/manual/en/function.json-decode.php

try it in some validator as this one http://jsonlint.com/

Ivan Montes
  • 91
  • 10
  • Looks like his original input was the string 'string(32) "{"telephone":"X XXX XXX XXX"}"' which is not JSON – GantTheWanderer Jan 20 '17 at 19:30
  • Thanks for the reponse. But actually, the $result is {"telephone":"X XXX XXX XXX"} – Silver Dragon Jan 20 '17 at 21:49
  • That NULL you have there. It should not be there or there should be a key whose value is NULL – Joseph Jan 20 '17 at 21:58
  • NULL doesn't mean that json_decode($result, true) is NULL witch means that it's not working? – Silver Dragon Jan 20 '17 at 22:03
  • NULL may mean that its empty and haven't been given a value or some other error between the script and getting that value. May i ask from where are you getting this json file? and can you put more of that json code? – Ivan Montes Jan 20 '17 at 22:09
  • Edit in the first message. This code works perfectly with an other curl on this website (hidden XXXXXXX.com). But with this result, witch seems to be a valid json array, json_decode doesn't work? – Silver Dragon Jan 20 '17 at 22:17
  • try commenting out this line. `curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true); ` check this previous question http://stackoverflow.com/questions/17016506/how-to-parse-json-response-from-curl – Ivan Montes Jan 20 '17 at 22:38
  • Without that, curl_exec() printed the response instead of returning it into $result, and $result just contains the value TRUE that was returned by curl_exec to indicate that it was successful. – Silver Dragon Jan 20 '17 at 23:05
1

The solution was that the response is in UTF-8 but not UTF-8 (SANS BOM). It means that there is a invisible caractere on the beginning of the string (U+FEFF).

The solution : $result = substr($result, 3);

Thanks for your help

Silver Dragon
  • 33
  • 1
  • 1
  • 5