1

Dear good people of Stack. I have a custom PHP web site and would like to ask for help in passing new user's name and emails to my GetResponse list. I currently have a Mailchimp integration, which works great, but would like to rewrite it for GetResponse (Please see the code below).

I've consulted this doc, but couldn't do it: https://apidocs.getresponse.com/v3/case-study/adding-contacts

Could someone please help me modify this to work with GetReponse please? I would be very thankful.

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

Thank you in advance!

Arsen K
  • 11
  • 1

1 Answers1

0

See code below, which works for me. I developed this to gather user's actual IP address (not shown in this example). The API URL is important: should end /contacts ($url below), but this is less than clear in Getresponse documentation. I'm not sure there is a 'status' field in Getresponse, so I have not included that (once added to contacts, the status will be subscribed).

<?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();
?>
  • Thank you so much Sheldon! I really appreciate it! Would you suggest adding this to my registration page, where I can pass the values using this code? Thank you in advance. – Arsen K Mar 14 '21 at 05:36
  • @Arsen I think it depends whether there are any steps after those on the registration page. If not, then add to that page. If there are later steps, to confirm registration, then add there. In my case, I have users register an account with my site (which uses UserSpice), then confirm by a link in an email, in other words, double opt-in. I have added the code to the 'activate account' page which users go to when clicking the link, since that is the final step. You should be careful with single opt-in as it is liable to bot sign-ups, which Getresponse can flag and suspend your account for. – Sheldon Smith Mar 15 '21 at 08:40
  • thank you very much for taking the time to help me out! I really appreciate it! – Arsen K Mar 17 '21 at 10:51