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

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

http request from website to cloud function in php

问题

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

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

这是我的云函数:

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

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

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

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

  1. 客户端错误:`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.

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

Here is my cloud function

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

But I am getting below error in cloud function

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

and below exception in my laravel screen

  1. 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外观...

所以,您的代码应该是:

  1. use Illuminate\Support\Facades\Http;
  2. Http::post(
  3. 'https://us-central1-myprojectid.cloudfunctions.net/updateOrderStatus',
  4. [
  5. 'form_params' => [
  6. 'orderId' => '2',
  7. 'newStatus' => 3,
  8. ]
  9. ]
  10. );
英文:

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

So, your code should be:

  1. use Illuminate\Support\Facades\Http;
  2. Http::post(
  3. 'https://us-central1-myprojectid.cloudfunctions.net/updateOrderStatus',
  4. [
  5. 'form_params' => [
  6. 'orderId' => '2',
  7. 'newStatus' => 3,
  8. ]
  9. ]
  10. );

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:

确定