简体   繁体   English

通过API订阅MailGun中的一批电子邮件地址

[英]Subscribing a batch of email addresses in MailGun via API

When sending a batch of new email addresses to subscribe to my mailing list, I get a fatal error. 发送一批新的电子邮件地址以订阅我的邮件列表时,出现致命错误。

I borrowed the code from the example under "Add multiple mailing list members (limit 1,000 per call)" on Mailgun's API documentation at https://documentation.mailgun.com/en/latest/api-mailinglists.html#examples and my array looks like the example. 我从https://documentation.mailgun.com/en/latest/api-mailinglists.html#examples和我的数组上Mailgun的API文档中“添加多个邮件列表成员(每个呼叫限制1,000个)”示例中的代码中借用了代码看起来像例子。

My array: 我的数组:

array:2 [▼
  "members" => array:1 [▼
    0 => "[{"name":"Someone","address":"sub1@example.com","vars":"{\"subscriber_id\":33}"},{"name":"Ellem","address":"sub2@example.com","vars":"{\"subscriber_id\":34}"},{"name":"Enno","address":"sub3@example.com","vars":"{\"subscriber_id\":35}"},{"name":"Nick","address":"sub4@example.com","vars":"{\"subscriber_id\":36}"},{"name":"Carl","address":"sub5@example.com","vars":"{\"subscriber_id\":37}"},{"name":"Tammy","address":"sub6@example.com","vars":"{\"subscriber_id\":38}"},{"name":"Janis","address":"sub7@example.com","vars":"{\"subscriber_id\":39}"}]"
  ]
  "upsert" => true
]

and the call I'm making: 和我打的电话:

$mgClient = new Mailgun($MAILGUN_SECRET);
$result = $mgClient->post("lists/$listAddress/members.json", $myarray);

I get the following error: 我收到以下错误:

First argument to Stream::create() must be a string, resource or StreamInterface. Stream :: create()的第一个参数必须是字符串,资源或StreamInterface。 {"userId":1,"email":"sub1@example.com","exception":"[object] (InvalidArgumentException(code: 0): First argument to Stream::create() must be a string, resource or StreamInterface. at /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/nyholm/psr7/src/Stream.php:87) {“ userId”:1,“电子邮件”:“ sub1@example.com”,“ exception”:“ [对象](InvalidArgumentException(code:0):Stream :: create()的第一个参数必须是字符串,资源或StreamInterface。位于/home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/nyholm/psr7/src/Stream.php:87)

Here's the part of the stack trace that seems relevant: 这是堆栈跟踪中似乎相关的部分:

/home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/nyholm/psr7/src/Factory/HttplugFactory.php(29): Nyholm\\Psr7\\Stream::create(true) /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/nyholm/psr7/src/Factory/HttplugFactory.php(29):Nyholm \\ Psr7 \\ Stream :: create(true)

#1 /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/php-http/multipart-stream-builder/src/MultipartStreamBuilder.php(61): Nyholm\\Psr7\\Factory\\HttplugFactory->createStream(true) #1 /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/php-http/multipart-stream-builder/src/MultipartStreamBuilder.php(61):Nyholm \\ Psr7 \\ Factory \\ HttplugFactory-> createStream(true)

#2 /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(98): Http\\Message\\MultipartStream\\MultipartStreamBuilder->addResource('upsert', true, Array) #2 /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(98):Http \\ Message \\ MultipartStream \\ MultipartStreamBuilder-> addResource('upsert' ,正确,数组)

#3 /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(179): Mailgun\\Connection\\RestClient->send('POST', 'lists/devtest@n...', Array, Array) #3 /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(179):Mailgun \\ Connection \\ RestClient-> send('POST','列表/ devtest @ n ...',数组,数组)

#4 /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(208): Mailgun\\Connection\\RestClient->post('lists/devtest@n...', Array, Array) #4 /home/SECRET.cloudwaysapps.com/SECRET/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(208):Mailgun \\ Connection \\ RestClient-> post('lists / devtest @ n。 ..',数组,数组)

#5 /home/SECRET.cloudwaysapps.com/SECRET/public_html/app/Http/Controllers/SubscribersController.php(591): Mailgun\\Mailgun->post('lists/devtest@n...', Array) #5 /home/SECRET.cloudwaysapps.com/SECRET/public_html/app/Http/Controllers/SubscribersController.php(591):Mailgun \\ Mailgun-> post('lists / devtest @ n ...',Array)

#6 [internal function]: App\\Http\\Controllers\\SubscribersController->processUpload(Object(Illuminate\\Http\\Request)) #6 [内部功能]:App \\ Http \\ Controllers \\ SubscribersController-> processUpload(Object(Illuminate \\ Http \\ Request))

I would appreciate any guidance on where I went wrong. 对于任何我做错地方的指导,我将不胜感激。 Was I supposed to CREATE and ATTACH the members.json file? 我是否应该创建并附加members.json文件?

I could not get it to work using the post method. 使用post方法无法正常工作。 Another person had trouble with a different post command, and the advice was to use the helpers, for which documentation is difficult to find. 另一个人在使用不同的post命令时遇到了麻烦,建议使用助手,因为很难找到相关文档。 But I cobbled together this solution: 但是我凑齐了这个解决方案:

First, I created an array for the new members: 首先,我为新成员创建了一个数组:

$sarray = [];
$sarray["name"] = $name;
$sarray["address"] = $email;
$sarray["vars"] = array('subscriber_id'=>$id);

and bundled them into an array of member arrays, $combinedarray 并将它们捆绑成成员数组$combinedarray

Then I called the createMultiple method: 然后,我调用了createMultiple方法:

    $mgClient = Mailgun::create($MAILGUN_API_KEY);
    $result = $mgClient->mailingList()->member()->createMultiple($listaddress,$combinedarray,'no');

The $result was an UpdateResponse object, which has a public getMessage method, so $message = $result->getMessage(); $ result是一个UpdateResponse对象,它具有公共的getMessage方法,因此$message = $result->getMessage();

got me what I needed. 给我我需要的东西。 Seems simple now that I've boiled it down, but FINDING this info was the challenge! 现在,我将其归结为简单的东西,但是查找此信息是一个挑战! (I think Mailgun is in the process of updating the API and docs, so this solution might be much more easily found in the docs soon.) (我认为Mailgun正在更新API和文档,因此很快可以在文档中更轻松地找到此解决方案。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM