簡體   English   中英

使用屬性將代碼卷曲到 PHP

[英]Curl code to PHP with properties

我有以下 Curl 代碼,我使用 curl to PHP 轉換器將其轉換為 PHP 代碼

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

我得到的代碼是

$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);

然而,這似乎並不正確,因為“屬性”行沒有被翻譯成 PHP 代碼。

如何將帶有 $first_name 變量的“屬性”行合並到我的 PHP 代碼中?

是的,這絕對不正確,轉換器搞砸了 CURLOPT_POSTFIELDS 行。

嘗試

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');

反而。 如果你能被激怒,請向轉換器的作者發送錯誤報告,這絕對是一個錯誤。

和 protip,在 php 中,您可能想使用 http_build_query 函數代替application/x-www-form-urlencoded -encoding 和 json_encode 用於 json 編碼,例如

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 

) ) );

另請注意,您確定在 curl 示例中正確使用了此 api 嗎? 它將application/x-www-form-urlencoded -encoding 與 json-encoding 混合在一起,這很不尋常。 如果正確,這是一個奇怪的設計選擇。 如果我是你,我會仔細檢查這是否是文檔的正確行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM