php - 带有请求正文的 Wordpress 发布 API
问题描述
我正在尝试使用他们的 API 向 klaviyo 发出 POST 请求wp_remote_post()
。这是他们的指南:
网址:POST https://a.klaviyo.com/api/v2/list/{LIST_ID}/members
示例请求:
{
"api_key": "api_key_comes_here",
"profiles": [
{
"email": "george.washington@example.com",
"example_property": "valueA"
},
{
"email": "thomas.jefferson@example.com",
"phone_number": "+12223334444",
"example_property": "valueB"
}
]
}
api_key: string您帐户的API密钥。
配置文件:JSON 对象列表您要添加到列表中的配置文件。列表中的每个对象都必须有一个 email、phone_number 或 push_token 键。您还可以提供其他属性作为键值对。
这是我尝试过的:
$profiles = ['email' => $content];
$args = ["api_key" => {API_key},
"profiles" => json_encode($profiles)
];
$res = wp_remote_retrieve_body( wp_remote_post( 'https://a.klaviyo.com/api/v2/list/{LIST_ID}/members', [
'body'=> $args
] ));
响应是:“无法解析配置文件”
我做错了什么,我该如何解决?
解决方案
您没有像示例请求似乎建议的那样对完整的请求正文进行编码
$args = [
"api_key" => 'some_api_key_string',
"profiles" => [
[
"email" => "john_doe@somewhere.com",
"value" => "some value",
],
[
"email" => "jane_doe@somewhere.com",
"value" => "some other value",
]
],
];
$listId = 123;
$url = "https://a.klaviyo.com/api/v2/list/{$listId}/members";
$response = wp_remote_post($url, json_encode($args));
这将为您提供示例中的输出
推荐阅读
- google-slides-api - 是否可以将形状从幻灯片移动到另一个?
- reactjs - Azure App Service 在部署 REACT JS 应用程序时出现错误
- reactjs - 获取 webTorrent 以播放存储在 AWS 私有 s3 存储桶中的 .mp4 文件
- bash - 当 Perl 嵌入 shell 脚本时,如何查看在父脚本中使用实际行号报告的语法错误?
- flutter - 弯曲的导航包在点击时重建页面
- tensorflow - 如何通过 ImageDataGenerator 在 Kaggle/Colab 上使用 TPU?
- javascript - 如何删除画布html中的点?
- reactjs - 在第二次单击时导出图形时获取属性“导出”未定义
- google-cloud-platform - Terraform GCP 数据目录策略标签
- python - 在 Python 中使用 Numpy 进行字节交换