如何在Guzzle上发帖?

时间:2018-08-18 10:12:20

标签: php laravel guzzle expo

在这里cra我的头。此代码有什么问题?出于某种原因,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

注意:在枪口文章中将'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...)

1 个答案:

答案 0 :(得分:1)

从上一个问题解决到第一个问题:

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

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

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

curl -H“内容类型:application / json” -X POST“ https://exp.host/--/api/v2/push/send” -d'{   “至”:“ ExponentPushToken [xxxxxxxxxxxxxxxxxxxxxx]”,   “ title”:“你好”,   “ body”:“世界” }'