首页 > 解决方案 > 通过 API 将电子邮件和姓名传递给 GetResponse

问题描述

亲爱的 Stack 的好人。我有一个自定义的 PHP 网站,并且想在将新用户名和电子邮件传递到我的 GetResponse 列表时寻求帮助。我目前有一个 Mailchimp 集成,效果很好,但想为 GetResponse 重写它(请参阅下面的代码)。

我已经咨询了这个文档,但做不到:https ://apidocs.getresponse.com/v3/case-study/adding-contacts

有人可以帮我修改它以使用 GetReponse 吗?我将非常感谢。

\Unirest\Request::auth('Arsen', 'MAILCHIMP_API_KEY');

$body = \Unirest\Request\Body::json([
    'email_address' => $_POST['email'],
    'merge_fields' => [
        'LNAME' => $_POST['name']
    ],
    'status' => 'subscribed'
]);

\Unirest\Request::post(
    'MAILCHIMP_LINK' . '/lists/' . 'MAILCHIMP_API_LIST' . '/members',
    [],
    $body
);

先感谢您!

标签: phpapirestmailchimpgetresponse

解决方案


请参阅下面的代码,这对我有用。我开发了这个来收集用户的实际 IP 地址(本例中未显示)。API URL 很重要:应该以 /contacts 结尾(下面的 $url),但这在 Getresponse 文档中不太清楚。我不确定 Getresponse 中是否有“状态”字段,所以我没有包含它(一旦添加到联系人,状态将被订阅)。

<?php
//API url
$url = 'https://api.getresponse.com/v3/contacts';

// Collection object
// Change YYYYY to campaign ID, called campaign_token in sign-up html forms
$post = ['dayOfCycle' => 0,'email' => $_POST['email'],'name' => $_POST['name'], 'campaign' => ['campaignId' => 'YYYYY']];

// Initializes a new cURL session
$curl = curl_init($url);
// Set the CURLOPT_RETURNTRANSFER option to true
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the CURLOPT_POST option to true for POST request
curl_setopt($curl, CURLOPT_POST, true);
// Set the request data as JSON using json_encode function
curl_setopt($curl, CURLOPT_POSTFIELDS,  json_encode($post));
// Set custom headers for X-Auth-Token, needed for Getresponse API
// Change ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ to actual API key
curl_setopt($curl, CURLOPT_HTTPHEADER, [
  'X-Auth-Token: api-key ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ',
  'Content-Type: application/json'
]);

// Execute cURL request with all previous settings
ob_start();
curl_exec($curl);
// close the connection, release resources used
curl_close($curl);      
ob_end_clean();
?>

推荐阅读