php - PHP cURL 不会将 JSON 发布到 API
问题描述
我有一个带有链接的仪表板,该链接触发数据库将应用程序状态更新为“已批准”并发布到 API。正在更新数据库,但未向收件人发送电子邮件。
数据库步骤正在运行,但未发布到 API。API 是触发向个人发送电子邮件的原因。
免责声明:我是编码新手,如果我没有提供正确/足够的信息,我深表歉意。
<?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";
}
?>
预期结果:使用新的应用程序状态更新数据库并触发电子邮件到 $data 中列出的电子邮件。
实际结果:它更新数据库但不发布到 API。
解决方案
推荐阅读
- mysql - 删除比另一个表中具有相同 ID 的行更旧的行
- google-drive-api - 如何获取集成到我的应用程序中的 Google Drive API 的使用统计信息
- android - AppBarLayout 扩展滚动范围
- javascript - Jquery Remove from onclick inside div
- excel - 基于单元格值显示的 Excel 图像
- php - Centos 6.7 PHP 从 PHP 5.2 升级到 5.6
- excel - VBA用文本值填充可变单元格范围
- android - React Native 调试器:异步存储
- python - 如何同时使用 Selenium 和 Scrapy?
- node.js - (Sequelize) hasOne 关联没有创建外键