Wordpress用户数据通过cURL发送

时间:2016-03-11 15:06:41

标签: wordpress curl user-data

我刚刚将营销工具Klaviyo与我们的Wordpress / WooCommerce设置集成在一起,并尝试通过cURL API推送用户元数据 - 但失败了!

您可以在此处了解API的工作原理:https://www.klaviyo.com/docs/http-api#people

我希望添加一个动作,这样当用户个人资料保存时,它会挂钩我的函数,将元数据发送到Klaviyo。

任何人都可以看到我做错了吗 - 请在下面编写代码?

非常感谢。

林茨

<?php

// Hook into the action which saves the User Meta Data (written by LD)

add_action( 'personal_options_update', 'klaviyo_send' );
add_action( 'edit_user_profile_update', 'klaviyo_send' );

function klaviyo_send () {

// Get cURL resource
$curl = curl_init();

// Set some options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://a.klaviyo.com/api/identify?data=eyJ0b2tlbiI6ICJoYXFYaXEiLCAicHJvcGVydGllcyI6IHsiJGVtYWlsIjogInRob21hcy5qZWZmZXJzb25AZXhhbXBsZS5jb20iLCAiJGxhc3RfbmFtZSI6ICJKZWZmZXJzb24iLCAiUGxhbiI6ICJUcmlhbCIsICJTaWduIFVwIERhdGUiOiAiMjAxMy0wMS0yNyAxMjoxNzowNiIsICIkZmlyc3RfbmFtZSI6ICJUaG9tYXMifX0=',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
    'token' => 'haqXiq', //This is the public 'key' in Klaviyo
    '$email' => $user_id->email, //This translates the wordpress field into the Klaviyo field
    'twitter' => $user_id->twitter,
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

}

?>

1 个答案:

答案 0 :(得分:0)

尝试通过这种方式发送数据。将发布的数据更改为友好的网址,例如?key=value&key=value&key=value

  //extract data from the post
    //set POST variables
    $url = 'https://a.klaviyo.com/api/identify?data=eyJ0b2tlbiI6ICJoYXFYaXEiLCAicHJvcGVydGllcyI6IHsiJGVtYWlsIjogInRob21hcy5qZWZmZXJzb25AZXhhbXBsZS5jb20iLCAiJGxhc3RfbmFtZSI6ICJKZWZmZXJzb24iLCAiUGxhbiI6ICJUcmlhbCIsICJTaWduIFVwIERhdGUiOiAiMjAxMy0wMS0yNyAxMjoxNzowNiIsICIkZmlyc3RfbmFtZSI6ICJUaG9tYXMifX0=';
    $fields = array(
        'token' => 'haqXiq', //This is the public 'key' in Klaviyo
        '$email' => $user_id->email, //This translates the wordpress field into the Klaviyo field
        'twitter' => $user_id->twitter,
    );

    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

    //execute post
    $result = curl_exec($ch);

    //close connection
    curl_close($ch);