0

I am trying to parse a JSON response from a server to a array, but it won't work. I am using: json_decode($string,true);

So that must not be the problem.

This is the code where I ask the server:

$curl_header = array();
$curl_header[] = "Authorization: Basic ".base64_encode("$auth_code:");

$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($curl, CURLOPT_URL, 'https://api.kassacompleet.nl/v1/ideal/issuers/');

$result = curl_exec($curl);

curl_close($curl);

// Handle and store result
$test = json_decode($result, true);

print_r($test);

This is the response i get from the server and after printing it:

[ { "id": "INGBNL2A", "list_type": "Nederland", "name": "Issuer Simulation V3 - ING" }, { "id": "RABONL2U", "list_type": "Nederland", "name": "Issuer Simulation V3 - RABO" } ]1

Also this strange nr 1 appears on the end of the string after i print it.

Does someone have any tips for me ?

1 Answer 1

1

$result is probably 1 because this is what curl_exec is returning. The HTTP response is being sent to PHP's output stream because you need to use the option CURLOPT_RETURNTRANSFER to get the result back in a variable.

Try this:

$curl_header = array();
$curl_header[] = "Authorization: Basic ".base64_encode("$auth_code:");

$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($curl, CURLOPT_URL, 'https://api.kassacompleet.nl/v1/ideal/issuers/');

// need to add this option to have curl_exec return the response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl); // result is now the response, before it was `true`

curl_close($curl);

// Handle and store result
$test = json_decode($result, true);

print_r($test);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.