1

I have a PHP file that triggers an API call when a user fills out a form on my site. At the end of the API call, it returns the response.

Where/how can I actually see this response? Filling out the form on the site runs the PHP file, but I'm not sure where it's actually outputting the response at.

Visting /myFile.php doesn't work because it's missing required inputs to actually run the API call.

Any help is much appreciated..

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.sendinblue.com/v3/smtp/email',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>json_encode($payload),
    CURLOPT_HTTPHEADER => array(
        'api-key: hidden',
        'Content-Type: application/json',
        'Cookie: __cfduid=dad4a689606c86c195e4d8ce33b53c5c51611684716'
    ),
));

$response = curl_exec($curl);

curl_close($curl);
return json_decode($response, true);
mickdeez
  • 507
  • 3
  • 7
  • 16
  • Please, add your code in your question. – Syscall Mar 05 '21 at 21:24
  • Updated @Syscall – mickdeez Mar 05 '21 at 21:25
  • how are you expecting this to be triggered, and where are you expecting `return json_de...` to return the result to? – imposterSyndrome Mar 05 '21 at 21:28
  • This gets triggered when someone fills out a form on my site - the user input fills in the $payload variable. The API call works fine. But I'm trying to make some slight changes that aren't working but I have no way of viewing the return response. So, I'm not sure where to expect the return to - which is my original question – mickdeez Mar 05 '21 at 21:29
  • pass the $response to error_log, or dump to file – Lawrence Cherone Mar 05 '21 at 21:31
  • @LawrenceCherone I tried doing error_log($response, 0); but nothing is showing up when I view the error_log on my server – mickdeez Mar 05 '21 at 21:35
  • 1
    can't you just var_dump the response and kill the script? - or make the request with something like Postman so you can view it more easily? – imposterSyndrome Mar 05 '21 at 21:48

1 Answers1

1

The Sendinblue documentation says the HTTP status code of the response is 201 or 400.

You can use curl_getinfo() with CURLINFO_HTTP_CODE to get the status.

You can check if the messageId is defined in the response.

You also need to check to cURL errors using curl_error().

$response = curl_exec($curl);

// Check cURL error
if ($response === false) {
    $error = curl_error($ch);
    die("Error $error"); // for example
}

// decode JSON
$reponseData = json_decode($response, true);

// Check HTTP status
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

// Status "Bad request"
if ($httpcode == 400) {
    $error = $reponseData['message'];
    die("Error $error"); // for example
}

// Status "Created"
if ($httpcode == 201) {
    if (! empty($reponseData['messageId'])) {
        // Message sent !
    }
}
Syscall
  • 19,327
  • 10
  • 37
  • 52