【发布时间】:2020-11-10 22:09:21
【问题描述】:
我想将使用 post man 测试的 http post 请求转换为 symfony 操作:
我想在 symfony 中将有效负载转换为 json 数组以将数据发送到 url:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Routing\Annotation\Route;
class PushNotificationController extends AbstractController
{
/**
* @Route("/api/push_notification", name="push_notification")
*/
public function index()
{
$httpClient = HttpClient::create();
$response = $httpClient->request('POST', 'https://fcm.googleapis.com/fcm/send', [
'headers' => [
// 'Accept' => 'application/json',
// "Content-Type" => "application/json; charset=UTF-8",
'Authorization' => 'Bearer token'
],
'json' => [
'notification' => [
'title' => 'Portugal vs. Denmark',
'message' => 'My Notification Message',
'body' => '5 to 1',
],
'token' => 'token'
],
]);
$statusCode = $response->getStatusCode();
// $statusCode = 200
$contentType = $response->getHeaders()['content-type'][0];
// $contentType = 'application/json'
$content = $response->getContent();
// $content = '{"id":521583, "name":"symfony-docs", ...}'
$content = $response->toArray();
// $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
return $content;
}
}
我收到了这个错误:
我认为这是关于 payload 的错误。请问有什么建议吗?
【问题讨论】:
-
我确定您的请求中有一个无效的 json,但是您可以使用 FCM 的捆绑包,而无需自己完成所有请求。
标签: symfony firebase-cloud-messaging payload symfony-http-client