Pass Email and Name to GetResponse via API

769 Views Asked by At

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!

1

There are 1 best solutions below

3
Sheldon Smith On

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();
?>