如何使用 Guzzle 执行 post?
How to do a post with Guzzle?
在这里抓耳挠腮。这段代码有什么问题?出于某种原因,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' inte the guzzle post 给我一个与 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) API 端点接受 POST 请求(不是 GET)。这些术语不可互换。因此端点无法识别您重载的 GET 查询字符串。
2) Multipart表单数据需要格式化为Multipart表单数据。你没有这样做。您刚刚将参数从 form_params.
切换为 multipart
3) 最后,让您开始的问题。您的 "To" 字段不应是数组。我认为您收到 404 错误,因为那里没有接受您发送的 json 消息的端点。尝试创建 json 消息,就像他们的 hello world curl 示例一样,看看您是否可以从端点获得正确的响应。
curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{
"to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"title":"hello",
"body": "world"
}'
在这里抓耳挠腮。这段代码有什么问题?出于某种原因,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' inte the guzzle post 给我一个与 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) API 端点接受 POST 请求(不是 GET)。这些术语不可互换。因此端点无法识别您重载的 GET 查询字符串。
2) Multipart表单数据需要格式化为Multipart表单数据。你没有这样做。您刚刚将参数从 form_params.
切换为 multipart3) 最后,让您开始的问题。您的 "To" 字段不应是数组。我认为您收到 404 错误,因为那里没有接受您发送的 json 消息的端点。尝试创建 json 消息,就像他们的 hello world curl 示例一样,看看您是否可以从端点获得正确的响应。
curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{ "to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]", "title":"hello", "body": "world" }'