I have the following Curl code, which I used the curl to PHP converter to convert to PHP code
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
The code I got was
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://a.klaviyo.com/api/v1/list/dqQnNW/members");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "api_key=API_KEY&email=george.washington@example.com&properties='{&confirm_optin=true");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
However this doesn't seem right because of the "properties" line which doesn't get translated into the PHP code.
How do I incorporate the "properties" line with a $first_name variable into my PHP code?
yeah it's definitely not correct, the converter screwed up the CURLOPT_POSTFIELDS line.
try
curl_setopt($ch,CURLOPT_POSTFIELDS,'api_key=API_KEY&email=george.washington@example.com&properties={ "$first_name" : "George", "Birthday" : "02/22/1732" }&confirm_optin=true');
instead. and if you can be arsed, send a bugreport to the author of the converter, this is definitely a bug.
and protip, in php, you might wanna use the http_build_query function instead for application/x-www-form-urlencoded
-encoding, and json_encode for json encodig, eg
curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query ( array (
'api_key' => 'API_KEY',
'email' => 'george.washington@example.com',
'properties' => json_encode ( array (
'$first_name' => 'George',
'Birthday' => '02/22/1732'
) ),
'confirm_option' => true
) ) );
also note, are you sure you're using this api correctly in your curl example? it's mixing application/x-www-form-urlencoded
-encoding with json-encoding, that's unusual. a weird design choice if correct. i'd double-check if that's the correct behavior with the docs if i were you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.