2

我有一个 PHP 文件,当用户在我的网站上填写表单时会触发 API 调用。在 API 调用结束时,它返回响应。

我在哪里/如何实际看到此响应?填写网站上的表单会运行 PHP 文件,但我不确定它实际输出响应的位置。

访问 /myFile.php 不起作用,因为它缺少实际运行 API 调用所需的输入。

任何帮助深表感谢..

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);
4

1 回答 1

2

Sendinblue文档说响应的 HTTP 状态代码是 201 或 400。

您可以使用curl_getinfo()withCURLINFO_HTTP_CODE来获取状态。

您可以检查是否messageId在响应中定义。

您还需要使用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 !
    }
}
于 2021-03-14T10:27:24.220 回答