Php 使用API将联系人添加到dotmailer通讯簿

Php 使用API将联系人添加到dotmailer通讯簿,php,api,nusoap,dotmailer,Php,Api,Nusoap,Dotmailer,我真的很头疼使用nusoap向dotmailer添加联系人。我正在使用AddContactToAddressBook方法,但无法使其工作。if语句返回success,但echo“”。打印($result,true)。"";不返回任何内容,当我检查dotmailer时,那里没有新联系人。我花了几个星期的时间试图让它工作,但没有任何成功,我现在不知道问题在哪里 <?php $email='test@apitest.com'; function subscribe($email, &$r

我真的很头疼使用nusoap向dotmailer添加联系人。我正在使用AddContactToAddressBook方法,但无法使其工作。if语句返回success,但
echo“”。打印($result,true)。"";不返回任何内容,当我检查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 />";
}
?>
<?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;
}
<?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;
}
?>

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

我为dotmailer工作,所以我可以先给你解释一下。您不能直接向通讯簿发送/添加电子邮件。您需要先将电子邮件发送到dotmailer,您将得到contact对象的响应。然后,您可以使用该联系人对象将您的电子邮件/联系人发布/添加到通讯簿

在下面,您将找到您需要做的全部工作示例。也可以通过此链接阅读api说明


我将自定义字段放在另一个数组中,现在可以使用了。这就是我现在得到的:



非常感谢您的帮助!!不过,只有一个问题,我们有很多自定义字段,如订单凭证,如何获取这些字段中的信息?我尝试向数据数组添加
'ORDER-VOUCHER'=>'123',
,但没有成功work@Cristik如何添加自定义字段?我想添加订户姓名和其他详细信息,如订单凭证代码。我试过:
$data=array('Email'=>'aitst2@apitest2.com“,//发送电子邮件到“EmailType”=>“Html”,//其他选项为纯文本“数据字段”=>数组('key'=>“订单凭证”,'value'=>“123',)但这不起作用