我正在尝试将示例curl请求写入PHP
POST https://a.klaviyo.com/api/v1/list/{{ LIST_ID }}/members
curl https://a.klaviyo.com/api/v1/list/dqQnNW/members \
-X POST \
-d api_key=API_KEY \
-d email=george.washington@example.com \
-d properties='{ "$first_name" : "George", "Birthday" : "02/22/1732" }' \
-d confirm_optin=true
这是我写的代码。我没有得到的部分是如何将电子邮件作为数据输入。
$data_string =
'{
"email": "'.$email.'"
}';
$ch = curl_init("https://a.klaviyo.com/api/v1/list/$form_id/members?api_key=$mail_api_key");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
)
);
我得到的错误是
Array
(
[status] => 400
[message] => This request is missing the following required params: "email".
)
答案 0 :(得分:0)
<?php
$email='test@gmmail.com';
$data_string =json_encode($email);
$form_id=5;
$mail_api_key=10;
$ch = curl_init("https://a.klaviyo.com/api/v1/list/$form_id/members?api_key=$mail_api_key");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
)
);
form_id和api_key我将它们设置为硬编码,以便我可以看到响应。这段代码可以正常工作,只需删除$ form_id和mail_api_key并像设置它们一样设置它们。
答案 1 :(得分:0)