【发布时间】:2021-09-30 04:39:45
【问题描述】:
我正在尝试为 firebase 构建一个函数,以通过 POST 方法调用命令上的 url。我目前已经很好地实现了 GET 方法,但是 POST 方法让我摸不着头脑。
我有一些通过 fetch 调用的示例代码,但我不确定下面这个 sn-p 中的参数需要去哪里:
<?php
$url = 'https://profootballapi.com/schedule';
$api_key = '__YOUR__API__KEY__';
$query_string = 'api_key='.$api_key.'&year=2014&week=7&season_type=REG';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
我的 POST 请求示例代码如下所示:
const apiKey = "myAPIkey";
const url = "https://profootballapi.com/schedule";
const response = await fetch(url, {
method: 'POST',
body: 'api_key'= apiKey, '&year=2018&week=7&season_typeRG';
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}});
if (!response.ok) {/* Handle */}
// If you care about a response:
if (response.body !== null) {
functions.logger.log(response.body);
}
【问题讨论】:
-
我认为这是一个寻求调试帮助的公平问题,但我认为标题会引起消极情绪。经常看到诸如“有人可以帮我转换这段代码吗?”之类的帖子。键入提问者不费力的问题。我很高兴看到您尝试自行转换它。我想不出更好的标题:-)
-
感谢您的反馈,长时间的听众,第一次来电大声笑。我会记住的!
标签: php typescript firebase google-cloud-functions