2016-01-06 75 views
0

我嘗試將聯繫人添加到我的Mailjet帳戶聯繫人列表,還有我的代碼:PHP:將聯繫人添加到列表mailjet V3簡單的API

<?php 

require('../mailjet_apiv3/src/Mailjet/php-mailjet-v3-simple.class.php'); 

$mj = new Mailjet($apiKey, $secretKey); 
$list_id =123; 
$params = array(
    'method' => 'POST', 
    'Email' => '[email protected]' 
); 
      $result = $mj->contact($params); 

      if(isset($result->StatusCode) && $result->StatusCode == '400') 
       return false; 

      $contact_id = $result->Data[0]->ID; 

      // Add the contact to a contact list 
      $params = array(
       'method' => 'POST', 
       'ContactID' => $contact_id, 
       'ListID' => $list_id 
      ); 
      $result = $mj->listrecipient($params); 
?> 

我得到這個錯誤: 注意:嘗試在第30行獲得.../addContactMailJet.php中的非對象的屬性。

我找不到問題,有人想法是什麼問題!

+0

哪一行是第30行? –

+0

@TomBrunberg這是第30行'$ contact_id = $ result-> Data [0] - > ID;' –

回答

0

狀態代碼訪問是這樣的:

$mj->_response_code 

因爲你也許一遍又一遍地嘗試POST在同一接觸,它可能已發回你的錯誤響應代碼。

但是,將聯繫人添加到列表的更好方法是使用contactslistManageContact資源。方法如下:

/** 
    * @param array $contact An array describing a contact. 
*        Example below the function. 
* @param int  $listID  The ID of the list. 
* 
*/ 
function addDetailedContactToList ($contact, $listID) 
{ 
    $mj = new Mailjet(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE')); 
    $params = array(
     "method" => "POST", 
     "Action" => "addnoforce", 
     "ID" => $listID 
    ); 

    // merge the contact informations with the call informations 
    $params = array_merge($params, $contact); 
    $result = $mj->contactslistManageContact($params); 

    if ($mj->_response_code == 201) 
     echo "success - detailed contact added to the list ".$listID; 
    else 
     echo "error - ".$mj->_response_code; 

    return $result; 
} 
// $contact array example 
/* $contact = array(
    *  "Email"   => "[email protected]", // Mandatory field! 
    *  "Name"   => "FooBar" 
    * ); 
    */ 
相關問題