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

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

最佳答案

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

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

关于php - 使用 API 将联系人添加到 dotmailer 地址簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29681030/

相关文章:

java - 从 PHP 向 Web 服务发送 HashMap 参数

php - 如何摆脱 "Uncaught SoapFault exception: [Client] looks like we got no XML document in..."错误

php - 更新时忽略唯一验证 ID

javascript - 如何从 Node 数据发送到React localStorage?

php - 使用 php nusoap 下载 mysql 表

c# - 针对 Windows 文件的服务器端病毒扫描

java - SpringBoot Catalina生命周期异常

php - "cURL error 77: error setting certificate verify locations"无明显原因

php - Mysql只显示日期之间的消息

php - 重命名不支持多字节字符