0

我正在尝试将 dotmailer 的 SOAP API 与 NUSOAP 一起使用,但我一点运气都没有。我可以连接到他们的服务器,但我根本无法获得任何信息。这是我到目前为止所得到的:

<?php
# initialise
$error_message="Something went wrong";
$wsdlPath = "https://apiconnector.com/v2/api.svc?wsdl";

# perform lookup
  require "http://www.domain.com/nusoap/nusoap.php";
  $client=new soapclient( $wsdlPath,'wsdl' );
  $client->setCredentials("username","password");
  $err=$client->getError();
  if( $err ) { echo $error_message; exit( $err ); }

  # create a proxy client.
  $proxy=$client->getproxy();
  $err=$proxy->getError();
  if( $err ) { echo $error_message; exit( $err ); }

  # call the SOAP method for GetAddressBooks.
  $result=$proxy->GetAddressBooksResponse->GetAddressBooksResult;
  if( $proxy->fault ) {
    echo $error_message;
  } else {
    $err=$proxy->getError();
    if( $err ) {
      echo $error_message; exit( $err );
    } else { 
      AddressBooks( $result );
    }
  }

  function AddressBooks( $result ) {
      print "<table>
<tr>
 <th></th>
 <th>Id</th>
 <th>Name</th>
 <th>Visibility</th>
 <th>Contact</th>
 </tr>";
  foreach( $result['addressBook'] as $key=>$addressbook ) {
 print "<tr>
 <td>" . $addressbook->Id . "</td>
 <td>" . $addressbook->Name . "</td>
 <td align='right'>" . $addressbook->Visibility . "</td>
 <td align='right'>" . $vaddressbook>Contacts . "</td>
 </tr>";
}
 print "</table>";
  }

  ?>

我唯一得到的是第一行。我自己和 dotmailer 谈过,但他们根本没有任何帮助。

4

1 回答 1

1

虽然这不是使用 SOAP 的完整答案,因为我最终使用了 REST,但这有效:

<?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;
}
?>
于 2015-05-12T11:52:23.930 回答