如何获取Mailjet v3 PHP包装器中的联系人ID? [英] How to get Contact ID in Mailjet v3 PHP wrapper?

查看:195
本文介绍了如何获取Mailjet v3 PHP包装器中的联系人ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Mailjet API将Mailjet列表中的提交的电子邮件存储起来。当有Mailjet 0.1版本的API(然后没有任何PHP包装,但是很容易使用他们的示例),但是当他们将API更改为版本3时,它们的PHP包装器在添加时不会返回任何联系人ID与联系人列表的新联系人。我早些时候提出过一个类似的问题,如何让Mailjet工作,但现在这个问题出现在新版本3.

I have used the Mailjet api to store submitted emails in a Mailjet list. This worked properly when there was the Mailjet 0.1 version API (then there wasn't any PHP wrapper but was easy to use with their examples), but when they changed the API to version 3 their PHP wrapper does not return any contact ID when adding a new contact to a contact list. I have raised a similar question earlier how to get Mailjet to work but now this issue arose with the new version 3.

这里再次修正了代码


*假设所显示的电子邮件是尚未在Mailjet中创建的新联系人。

*Assume that the shown email is a new contact that's not already created in Mailjet.



$mj = new Mailjet();
$contact_params = array("method" => "POST", "Email" => "abc@gmail.com");
$contact = $mj->contact($contact_params);

$add_params = array(
    "method" => "POST",
    "ContactID" => $contact,
    "ListID" => _MAILJET_LIST,
    "IsActive" => "True"
);
$mj->listrecipient($add_params);

开发人员@Gormador表示使用 $ contact->数据[0] - > ID 作为联系人ID,但仍然返回NULL。

Developer @Gormador said to use $contact->Data[0]->ID as contact id but it still returns NULL.

尝试将创建的联系人添加到列表时收到此错误调试模式设置为2)。

I get this error when trying to add a created contact to a list (with debug mode set to 2).

array(3) { ["ContactID"]=> NULL ["ListID"]=> string(1) "2"
["IsActive"]=> string(4) "True" } string(105) "{ "ErrorInfo" : "",
"ErrorMessage" : "Invalid data: \"null\" is an invalid integer",
"StatusCode" : 400 }"

上一个问题: Mailjet:添加电子邮件到列表不起作用

Previous question: Mailjet: Adding email to list does not work

更新:具有更正的完整代码,

Updated: Full code with corrections,

$mj = new Mailjet(); 
$add_email = "testing" . rand(0, 1000) . "@gmail.com"; 
$contact_params = array("method" => "POST", "Email" => $add_email); 
$mj->contact($contact_params); 
$viewContact = array("method" => "VIEW","unique" => $add_email); 
$contact = $mj->contact($viewContact); 
$add_params = array( "method" => "POST", "ContactID" => $contact->Data[0]->ID, "ListID" => 1, "IsActive" => "True" ); 
$mj->listrecipient($add_params);


推荐答案

编辑:



这个问题并不一定针对您尝试实现的目标。

尽管我们的1.0.6封装中肯定有一个错误。
在包装代码中,我们使用仅在PHP 5.4中引入的 json_decode()函数的第4个参数(请参见更改日志部分
here )。

This issue isn't necessarily specific to the goal you try to achieve.
Although, there definitely is a bug in our 1.0.6 wrapper. In the wrapper code, we use the 4th parameter of the json_decode() function, which was only introduced in PHP 5.4 (See the "Changelog" section here).

两种方法可以解决这个问题:

Two ways you can solve this:


  • 将系统的PHP版本更新为> = 5.4

  • 或者(直到我们在我们身边引入一个修复),在本地修改包装器以不使用该选项(请参阅 here ), 这很可能带来新的问题,推荐的修正

  • Either update your system's PHP version to >= 5.4
  • Or (until we introduce a fix on our side), modify the wrapper locally to not use that option (see here), which very well might bring new issues and isn't the recommended fix.

希望这有帮助。我们非常感谢这引起了我们的注意:)

Hope this helped. We certainly are grateful that this brought that particular issue to our attention :)

这是第二种方法在代码方面的意义:

To clarify, here's what the second approach means in term of code:

$this->_response = json_decode($buffer, false, 512, JSON_BIGINT_AS_STRING);
//Becomes
$this->_response = json_decode($buffer, false, 512);

应该消除警告来解决问题。不是很好吗? ; - )

That should eliminate the warning and solve the issue. Isn't that great?! ;-)

由于我真的无法在最后重现您的问题,以下是一个问题: >
当您发出以下代码时,您尝试创建的联系人是否在响应中列出?

或者,转到这里列出您帐户的联系人。

As I really am not able to reproduce your issue on my end, here is a question:
Is the contact you are trying to create listed in the response when you issue the following code ?
Alternatively, go here for a list of your account's contacts.

$mj = new Mailjet($apiKey, $secretKey);
$contact = $mj->contact();

var_dump($contact);

如果是这样,这就是为什么我在我的您对其他问题的回复不起作用。

If it is, this is why what I wrote in my response on your other question doesn't work.

嗯,你正在尝试创建一个已经在你的帐户中列出的联系人。创建联系人的过程不一定意味着必须分配给联系人列表。

Well, you are trying to create a contact that is already listed in your account. The process of creating a contact doesn't necessarily mean that it has to be assigned to a contacts list.

在将其添加到列表中之前,请先获取现有联系人的ID而不是创建它:

Either get the existing contact's id instead of creating it before adding it to a list:

$mj = new Mailjet($apiKey, $secretKey);

$viewContact = [
    "method"    =>  "VIEW",
    "unique"    =>  "gaetan.philippot@gmail.com"
];

$contact = $mj->contact($viewContact);

echo "Contact ID: ".$contact->Data[0]->ID."\n";

// Then proceed as described in my original answer.

或通过网页界面,然后再次尝试跟随我的答案。

(点击联系人的电子邮件,然后点击其头像旁边的黄色菜单,点击删除。)

Or delete it through the web interface and try following my answer again.
(Click on the contact's email, then click on the yellow menu next to its avatar and click on remove.)

解决您的问题? : - )

Does that solve your issue? :-)

这篇关于如何获取Mailjet v3 PHP包装器中的联系人ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆