php:添加联系人列表mailjet V3 simple Api

时间:2016-01-06 13:57:40

标签: php api mailjet

我尝试将联系人添加到我的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中获取非对象的属性。

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

1 个答案:

答案 0 :(得分: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"
  *  );
  */