【问题标题】:How to do a post with Guzzle? [duplicate]如何使用 Guzzle 发帖? [复制]
【发布时间】:2019-01-25 05:03:27
【问题描述】:

在这里挠头。这段代码有什么问题?出于某种原因,Expo 的响应是 404。我以错误的方式向他们发送东西,但不知道该怎么做!

EXPO 文档在这里:https://docs.expo.io/versions/latest/guides/push-notifications#http2-api

我的代码:

$client = new Client(); //GuzzleHttp\Client

    $url = 'https://exp.host/--/api/v2/push/send';

    $data = [
        'to' => array('ExponentPushToken[gaXXXXXXXXX_Oi4J1b9OR]'),
        'title' => $notification->title,
        'body' => $notification->body
    ];
    $headers = [
        'Accept' => 'application/json',
        'Accept-Encoding' => 'gzip, deflate',
        'Content-Type' => 'application/json',
    ];

    $response = $client->post($url, ['form_params' => $data, 'headers' => $headers]);


    dd($response);

得到以下错误:

Client error: `POST https://exp.host/--/api/v2/push/send` resulted in a `404 Not Found` response: Not Found

注意:在 guzzle 帖子中将 'form_params' 更改为 'multipart' 会给我带来与 expo 不同的错误:

A 'contents' key is required

最后,将 'form_params' 更改为 'query' 给了我:

Client error: `POST https://exp.host/--/api/v2/push/send?to%5B0%5D=ExponentPushToken%5XXXXXXXXXX_Oi4J1b9OR%5D&title=asdasd&body=asdasdasd` resulted in a `400 Bad Request` response: {"errors":[{"code":"API_ERROR","message":"child \"to\" fails because [\"to\" is required], \"value\" must be an array."} (truncated...)

【问题讨论】:

  • 你用的是什么版本的 Guzzle?
  • @Ross,最新的,所以 Guzzle 6> 我想...

标签: php laravel guzzle expo


【解决方案1】:

从最后一个到第一个解决您的问题:

1) API 端点接受 POST 请求(不是 GET)。这些术语不可互换。因此端点无法识别您重载的 GET 查询字符串。

2) Multipart 表单数据需要格式化为 Multipart 表单数据。你还没有这样做。您刚刚将参数从 form_params 切换为 multipart。

3) 最后,让你开始的问题。您的“收件人”字段不应是数组。我认为您收到 404 错误,因为那里没有接受您发送的 json 消息的端点。尝试像他们的 hello world curl 示例一样创建 json 消息,看看是否可以从端点获得正确的响应。

curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{ "to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxxxx]", “标题”:“你好”, “身体”:“世界” }'

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 2023-03-10
    • 2016-06-15
    • 2013-06-01
    • 2019-12-12
    • 2017-04-21
    • 2015-08-25
    • 2019-01-21
    相关资源
    最近更新 更多