简体   繁体   中英

Adding contact to dotmailer address book using API

I'm having a real headache adding a contact to to dotmailer using nusoap. I'm using the AddContactToAddressBook method,but I can't get it to work. The if statement returns success, but echo "<pre>" . print_r($result, true) . "</pre>"; echo "<pre>" . print_r($result, true) . "</pre>"; returns nothingand when I check dotmailer there's no new contact there. I've spent weeks trying to get this to work without any success and am at a loss now as to where the problem is!

<?php
$email='test@apitest.com';
function subscribe($email, &$result)
{
    $wsdlPath = "https://apiconnector.com/v2/api.svc?wsdl";

    $client=new soapclient( $wsdlPath,'wsdl' );
    $client->setCredentials("apiuser-xxxxxxxx@apiconnector.com","xxxxxx");
    $error = $client->getError();
    $result = $client->call('AddContactToAddressBook',array('addressBookId'=>xxxxxx,'email'=>'test@apitest.com'));

    if($client->fault) {
        $rv = false;
    } else {
        // Check for errors
        if($error) {
            $rv = false;
        } else {
            $rv = true;
        }
    }
    return $rv;
}
          echo "<pre>" . print_r($result, true) . "</pre>";

if(subscribe("test@test.com", $result)) {
    echo "success<br />";
    print_r($result);
} else {
    echo "failed<br />";
}
?>

I put the custom fields in another array and that now works. This is what I've now got:

<?php

/** POST EMAIL FIRST AND GET CONTACT FROM DOTMAILER */
$postContactUrl = 'https://apiconnector.com/v2/contacts/';
$data = array(
    'Email' => 'test@apitest.com', //email to post
    'EmailType' => 'Html', //other option is PlainText
    'dataFields' => array(
    array(
    'Key' => 'CITY',
    'Value' => $_POST['address2']),
    array(
    'Key' => 'COUNTRY',
    'Value' => $country_name),
    array(
    'Key' => 'FIRSTNAME',
    'Value' => $_POST['name_first']),
    array(
    'Key' => 'FULLNAME',
    'Value' => $_POST['name_first']." ".$_POST['name_last'] ),
    array(
    'Key' => 'LASTNAME',
    'Value' => $_POST['name_last']),
    array(
    'Key' => 'POSTCODE',
    'Value' => $_POST['postcode']),
    array(
    'Key' => 'STREET',
    'Value' => $_POST['address1']),
    array(
    'Key' => 'TOWN',
    'Value' => $_POST['address3']),
    )
);
//post email and response will be contact object from dotmailer
$contact = execute_post($postContactUrl, $data);

/** ADD CONTACT TO ADDRESS BOOK */
$addContactToAddressBookUrl = 'https://apiconnector.com/v2/address-books/' . 'address-book-id' . '/contacts'; //add your address book id
//post contact to address book and response will be address book object from dotmailer
$book =  execute_post($addContactToAddressBookUrl, $contact);

/**
 * if you want to check if there was an error you can
 * check it by calling this on response. 
 * example $book->message
 */

 echo "<pre>" . print_r($data, true) . "</pre>";
// echo "<pre>" . print_r($contact, true) . "</pre>";
// echo "<pre>" . print_r($book, true) . "</pre>";

//Function to initiate curl, set curl options, execute and return the response
function execute_post($url, $data){
    //encode the data as json string
    $requestBody = json_encode($data);

    //initialise curl session
    $ch = curl_init();

    //curl options
    curl_setopt($ch, CURLAUTH_BASIC, CURLAUTH_DIGEST);
    curl_setopt($ch, CURLOPT_USERPWD, 'user-name' . ':' . 'password'); // credentials
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . 'application/json' ,'Content-Type: application/json'));

    //curl execute and json decode the response
    $responseBody = json_decode(curl_exec($ch));

    //close curl session
    curl_close($ch);

    return $responseBody;
}
?>

You made a couple of mistakes in your script. Class name is SoapClient not soapclient. You will also need to use curl to accomplish what you want to do.

I work for dotmailer so i can explain you a bit first. You cannot post/add an email to address book directly. You need to post the email to dotmailer first, you will get contact object in response. Than you can use that contact object to post/add you email/contact to address book.

Below you will find complete fully working example of what you will need to do. Also follow this link to read api description https://apiconnector.com/v2/help/wadl

<?php

/** POST EMAIL FIRST AND GET CONTACT FROM DOTMAILER */
$postContactUrl = 'https://apiconnector.com/v2/contacts/';
$data = array(
    'Email' => 'test@apitest.com', //email to post
    'EmailType' => 'Html', //other option is PlainText
);
//post email and response will be contact object from dotmailer
$contact = execute_post($postContactUrl, $data);

/** ADD CONTACT TO ADDRESS BOOK */
$addContactToAddressBookUrl = 'https://apiconnector.com/v2/address-books/' . 'address-book-id' . '/contacts'; //add your address book id
//post contact to address book and response will be address book object from dotmailer
$book =  execute_post($addContactToAddressBookUrl, $contact);

/**
 * if you want to check if there was an error you can
 * check it by calling this on response. 
 * example $book->message
 */

echo "<pre>" . print_r($contact, true) . "</pre>";
echo "<pre>" . print_r($book, true) . "</pre>";

//Function to initiate curl, set curl options, execute and return the response
function execute_post($url, $data){
    //encode the data as json string
    $requestBody = json_encode($data);

    //initialise curl session
    $ch = curl_init();

    //curl options
    curl_setopt($ch, CURLAUTH_BASIC, CURLAUTH_DIGEST);
    curl_setopt($ch, CURLOPT_USERPWD, 'user-name' . ':' . 'password'); // credentials
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . 'application/json' ,'Content-Type: application/json'));

    //curl execute and json decode the response
    $responseBody = json_decode(curl_exec($ch));

    //close curl session
    curl_close($ch);

    return $responseBody;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM