我正在尝试使用他们的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": "[email protected]",
"example_property": "valueA"
},
{
"email": "[email protected]",
"phone_number": "+12223334444",
"example_property": "valueB"
}
]
}
api_key:字符串您帐户的API密钥。
配置文件:JSON对象列表要添加到列表中的配置文件。列表中的每个对象都必须具有电子邮件,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
] ));
响应是:“无法解析配置文件”
我在做什么错,我该如何解决?
最后,我找到了解决方案:
#1:在标题中添加“内容类型” => application / json
#2:将配置文件数组强制放入对象-由于行会说:profiles参数是JSON对象列表
工作代码:
$args = ["api_key" => "your_API_key",
"profiles" => array(
(object)['email' => '[email protected]']
)
];
$res = wp_remote_retrieve_body( wp_remote_post( 'https://a.klaviyo.com/api/v2/list/you_list_ID/members', [
'headers' => ['Content-Type' => 'application/json'],
'body' => json_encode($args)
]));
感谢@ j4g0为您提供的所有帮助。周末愉快!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句