简体   繁体   English

PHP cURL不会将JSON发布到API

[英]PHP cURL won't POST JSON to API

I have a dashboard with a link that triggers a DB to update an application status to "approved" and posts to an API. 我有一个带有链接的仪表板,该链接触发数据库将应用程序状态更新为“已批准”并发布到API。 The database is being updated, but the recipient is not being emailed. 正在更新数据库,但未通过电子邮件发送收件人。

The database step is working but not posting to the API. 数据库步骤正在运行,但无法发布到API。 The API is what triggers an email to the individual. 该API触发向个人发送电子邮件。

Disclaimer: I'm brand new to coding so apologies if I'm not providing the correct/enough information. 免责声明:如果您未提供正确/足够的信息,我是编码的新手,因此深表歉意。

<?php

require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db); //Connect DB
$id = $_GET['id'];
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error()); 
  }

//Create query based on the ID passed from table
// sql to update a record
$sql = "UPDATE all_contacts SET status='Approved' WHERE id = '$id'"; 

$url = 'https://mail.com/messages/email';
$data = array(
  "subject" => "Hello!",
  "body" => "hi",
  "recipients" => array (
    "email" => "example@email.com")
  );
$data_string = json_encode($data);
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "X-AUTH-TOKEN: mytoken",
    "Content-Length: ". strlen($data_string)));

$result = curl_exec($ch);
echo $result;
curl_close($ch);

if (mysqli_query($conn, $sql)) {
    mysqli_close($conn);
    header("Location: dashboard.php"); 
    exit;
} else {
    echo "Error changing status";
}

?>

Expected result: Update the DB with the new application status and trigger an email to the email listed in $data. 预期结果:用新的应用程序状态更新数据库,并触发一封电子邮件到$ data中列出的电子邮件。

Actual result: It updates the DB but does not post to the API. 实际结果:它更新数据库,但不发布到API。

Tested your code, and it's working correctly. 测试了您的代码,它可以正常工作。

I tested it to Beeceptor. 我对Beeceptor进行了测试。

在此处输入图片说明

在此处输入图片说明

What are you getting as an error? 作为错误您得到了什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM