drpph80800 2019-07-23 14:59
浏览 157
已采纳

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

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.

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. {"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)

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)

#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)

#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)

#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)

#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))

I would appreciate any guidance on where I went wrong. Was I supposed to CREATE and ATTACH the members.json file?

  • 写回答

1条回答

  • dtgu21994537 2019-07-23 20:42
    关注

    I could not get it to work using the post method. Another person had trouble with a different post command, and the advice was to use the helpers, for which documentation is difficult to find. 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

    Then I called the createMultiple method:

        $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();

    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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败