0

我尝试将联系人添加到我的 Mailjet 帐户中的联系人列表中,有我的代码:

<?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' =>  'test@tst.tst'
);
            $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 中获取非对象的属性。

我找不到问题,请有人知道出了什么问题!

4

1 回答 1

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"         =>  "foo@bar.com",   // Mandatory field!
  *      "Name"          =>  "FooBar"
  *  );
  */
于 2016-01-07T09:08:58.747 回答