API CURL Problem

Talvess

New member
Hello

Unfortunately I can't get any further

I have an API call (CURL) which should return a JSON and store the response in a variable $resp
with a var_dump($resp); I get this displayed
This also seems to contain a JSON but how do I get it?
The first part of the text does not seem to be part of the JSON. Can anyone help?
i need the JSON

string(529) "{"status":"SUCCESS","statuses":[{"id":35012,"name":"Neue Bestellungen","name_for_customer":"Neue Bestellung angenommen","color":"#69b6fa"},{"id":35013,"name":"Zum Senden","name_for_customer":"Bereit zum Versenden","color":"#EA864D"},{"id":35014,"name":"abgeschickt","name_for_customer":"Produkte wurden versendet","color":"#22A564"},{"id":35015,"name":"storniert","name_for_customer":"Die Bestellung wurde storniert","color":"#D54839"},{"id":35016,"name":"Beispielstatus","name_for_customer":"Beispielstatus","color":"#4E565F"}]}"



the PHP:
$methodParams = '[]';
$apiParams = [
"method" => "getOrders",
"parameters" => $methodParams
];
$curl = curl_init("https://xxxxx");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, [xxx]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($apiParams));
$response = curl_exec($curl);
curl_close($curl);

var_dump($response);


Tranbslate by deepl :)
 
Hi,

If you wish to print JSON, use echo to print JSON, e.g. echo $response, rather than var_dump.

Moreover, use if you require an array from JSON.
$responseData = json_decode($response, true);
echo "<pre>"; print_r($responseData);

i hope it will work for you.
 
Thank you for your answer

now the result is:
{"status":"SUCCESS","statuses":[{"id":35012,"name":"Neue Bestellungen","name_for_customer":"Neue Bestellung angenommen","color":"#69b6fa"},{"id":35013,"name":"Zum Senden","name_for_customer":"Bereit zum Versenden","color":"#EA864D"},{"id":35014,"name":"abgeschickt","name_for_customer":"Produkte wurden versendet","color":"#22A564"},{"id":35015,"name":"storniert","name_for_customer":"Die Bestellung wurde storniert","color":"#D54839"},{"id":35016,"name":"Beispielstatus","name_for_customer":"Beispielstatus","color":"#4E565F"}]}
1

i think "{"status":"SUCCESS","statuses" -> are not a part of JSON and the "1" at the end.

what i need is like this:
{
"id":35012,
"name":"Neue Bestellungen",
"name_for_customer":"Neue Bestellung angenommen",
"color":"#69b6fa"},
{
"id":35013,
"name":"Zum Senden",
"name_for_customer":"Bereit zum Versenden",
"color":"#EA864D"},
{
"id":35014,
"name":"abgeschickt",
"name_for_customer":"Produkte wurden versendet",
"color":"#22A564"},
{
"id":35015,
"name":"storniert",
"name_for_customer":"Die Bestellung wurde storniert",
"color":"#D54839"},
{
"id":35016,
"name":"Beispielstatus",
"name_for_customer":"Beispielstatus",
"color":"#4E565F"
}
 
Sorry, I found my mistake myself. It was missing
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

After that I got the JSON

Thanks for your help anyway
 
Back
Top