2015-04-16 36 views
2

我真的很頭痛,添加聯繫人到使用nusoap的歌迷。我使用的方法是AddContactToAddressBook,但我無法讓它工作。 if語句返回成功,但echo "<pre>" . print_r($result, true) . "</pre>";不返回任何內容,當我檢查點播程序時,那裏沒有新聯繫人。我花了好幾周的時間試圖讓這個工作沒有取得任何成功,並且現在已經不知所措了!使用API​​添加聯繫人到歌迷通訊錄

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

    $client=new soapclient($wsdlPath,'wsdl'); 
    $client->setCredentials("[email protected]","xxxxxx"); 
    $error = $client->getError(); 
    $result = $client->call('AddContactToAddressBook',array('addressBookId'=>xxxxxx,'email'=>'[email protected]')); 

    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("[email protected]", $result)) { 
    echo "success<br />"; 
    print_r($result); 
} else { 
    echo "failed<br />"; 
} 
?> 

回答

2

我把自定義字段在另一個陣列和現在的作品。這就是我現在得到的:

<?php 

/** POST EMAIL FIRST AND GET CONTACT FROM DOTMAILER */ 
$postContactUrl = 'https://apiconnector.com/v2/contacts/'; 
$data = array(
    'Email' => '[email protected]', //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

您在腳本中犯了一些錯誤。類名稱是SoapClient而不是soapclient。你還需要使用curl來完成你想要做的事情。

我爲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' => '[email protected]', //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; 
} 
+0

非常感謝你的工作!只是有一個問題,我們有很多自定義字段,例如ORDER-VOUCHER,我如何獲得這些字段中的信息?我試圖在數據數組中添加'ORDER-VOUCHER'​​=>'123','但這不起作用 –

+0

@Cristik我如何添加自定義字段?我想添加訂戶名稱和其他詳細信息,如訂單憑證代碼。我試過這個:'$ data = array( 'Email'=>'[email protected]',//發郵件到 'EmailType'=>'Html',//其他選項是PlainText 'datafields'= >'array'( \t'key'=>'order-voucher', \t'value'=>'123',) \t);'但那不起作用 –

相關問題