HTTP请求从网站到云函数的PHP代码

huangapple go评论84阅读模式
英文:

http request from website to cloud function in php

问题

我想要从我的 PHP 网站触发一个 HTTP 云函数,以更新 Firestore 记录。

$url = 'https://us-central1-myprojectid.cloudfunctions.net/updateOrderStatus';
$client = new Client();
$response = $client->post($url, [
    'form_params' => [
        'orderId' => '2',
        'newStatus' => 3,
    ]
]);

这是我的云函数:

exports.updateOrderStatus = functions.https.onCall(async (data, context) => {
  console.log("Inside updateOrderStatus function's onCall");
  console.log(data);
});

但是我在云函数中收到以下错误:

请求的 Content-Type 不正确。应为 application/x-www-form-urlencoded

并且在我的 Laravel 屏幕中收到以下异常:

客户端错误:`POST https://us-central1-myprojectid.cloudfunctions.net/updateOrderStatus` 导致 `400 Bad Request` 响应:{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}
英文:

I want to trigger a HTTP cloud function from my php website to update a firestore record.

  $url = 'https://us-central1-myprojectid.cloudfunctions.net/updateOrderStatus';
        $client = new Client();
        $response = $client->post($url, [
            'form_params' => [
                'orderId' => '2',
                'newStatus' => 3,
            ]
        ]);

Here is my cloud function

exports.updateOrderStatus = functions.https.onCall(async (data, context) => {
  console.log("Inside updateOrderStatus function's onCall");
  console.log(data);
  }
);

But I am getting below error in cloud function

Request has incorrect Content-Type. application/x-www-form-urlencoded

and below exception in my laravel screen

Client error: `POST https://us-central1-myprojectid.cloudfunctions.net/updateOrderStatus` resulted in a `400 Bad Request` response: {"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

答案1

得分: 0

如果您使用的是Laravel 7+,请使用Http外观...

所以,您的代码应该是:

use Illuminate\Support\Facades\Http;

Http::post(
    'https://us-central1-myprojectid.cloudfunctions.net/updateOrderStatus',
    [
        'form_params' => [
            'orderId' => '2',
            'newStatus' => 3,
        ]
    ]
);
英文:

If you are using Laravel 7+, use the Http facade...

So, your code should be:

use Illuminate\Support\Facades\Http;

Http::post(
    'https://us-central1-myprojectid.cloudfunctions.net/updateOrderStatus',
    [
        'form_params' => [
            'orderId' => '2',
            'newStatus' => 3,
        ]
    ]
);

huangapple
  • 本文由 发表于 2023年7月3日 02:51:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定