php - 将成员添加到 MailChimp 列表时出现 400 错误请求

标签 php laravel-5 guzzle mailchimp-api-v3.0

我正在向 the following resource 发送一个 POST 请求并获得 400。我明白 what the error means ,但仍然不确定为什么当对同一资源的 GET 请求有效时我会收到它。

/lists/{list_id}/members

这是代码的摘录:

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', // <-- Drop in a GET here and it works, other than it's not the behavior I need.
    env('MAILCHIMP_API_URL') . 'lists/' . env('MAILCHIMP_LIST_KEY') . '/members',
    [
        'auth'  => ['app', env('MAILCHIMP_API_KEY')],
        'query' => [
            'email_address' => 'donnie@test.com',
            'email_type'    => 'html',
            'status'        => 'subscribed',
        ]
    ]);

dd($response->getStatusCode());

回应

Client error: `POST https://XXXX.api.mailchimp.com/3.0/lists/XXXX/members?email_address=donnie%40test.com&email_type=html&status=subscribed`
resulted in a `400 Bad Request`
response: {
  "type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
  "title": "Invalid Resource",
  "status": 400,
  "detail": "The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
  "instance": "f32e7076-b970-4f5c-82c6-eec5875e83b4",
  "errors": [{
    "field": "",
    "message": "Schema describes object, NULL found instead"
  }]
}

最佳答案

您正在发送带有query 参数的POST 请求。您需要发送JSON编码的正文!

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', // <-- Drop in a GET here and it works, other than it's not the behavior I need.
    env('MAILCHIMP_API_URL') . 'lists/' . env('MAILCHIMP_LIST_KEY') . '/members',
    [
        'auth'  => ['app', env('MAILCHIMP_API_KEY')],
        'json' => [
            'email_address' => 'donnie@test.com',
            'email_type'    => 'html',
            'status'        => 'subscribed',
        ]
    ]);

dd($response->getStatusCode());

关于php - 将成员添加到 MailChimp 列表时出现 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46456722/

相关文章:

php - 如何使用 Guzzle 执行并发 POST 请求?

php - 不确定如何在 Javascript/jQuery 中解析这个 PHP/JSON 对象

html - 如何在 bootstrap 4 laravel 5 中获得一个好的文件上传按钮?

php - 更新 max_input_vars 后仍收到 "Input Variables Exceeded 1000"错误

php - 如何在 blade.php 中获取输入值

laravel - 无法在位置 24 (6) 解析时间字符串 (2018-03-10 13 :08:09. -708656):意外字符

http - Firebase REST API - Guzzle PATCH 请求正在删除数据

php - 如何在微软的php graph api中访问响应数据

php - SQL查询在phpmyadmin中工作但在php中不起作用

php - 哎呀,有表演吗?