使用API​​将联系人添加到dotmailer地址簿

时间:2015-04-16 16:40:28

标签: php api nusoap dotmailer

我真的很头疼,使用nusoap向dotmailer添加联系人。我正在使用 AddContactToAddressBook 方法,但我无法使其工作。 if语句返回成功,但echo "<pre>" . print_r($result, true) . "</pre>";没有返回任何内容,当我查看dotmailer时,那里没有新联系。我花了几周的时间试图让这个工作没有任何成功,现在我不知道问题出在哪里了!

<?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 />";
}
?>

2 个答案:

答案 0 :(得分:3)

我将自定义字段放在另一个数组中,现在可以正常工作了。这就是我现在所拥有的:

<?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;
}
?>

答案 1 :(得分:1)

你在剧本中犯了几个错误。类名是SoapClient而不是soapclient。您还需要使用curl来完成您想要做的事情。

我为dotmailer工作,所以我可以先解释一下。您无法直接向地址簿发送/添加电子邮件。您需要先将电子邮件发送到dotmailer,您将获得联系对象作为回应。您可以使用该联系对象将电子邮件/联系人发布/添加到地址簿。

下面您将找到完整的工作示例,说明您需要做什么。也请点击此链接阅读api说明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;
}