【发布时间】:2021-09-22 06:07:30
【问题描述】:
我有一个如下所示的 API 网址:
“https://cloud.XXXXXXXXX.com/api/data/XXXXXXX2017112020-804?from=2021-1-1T00:00:00&to=2021-2-26T00:00:00&averagingperiod=60&includejournal=false”
Api 使用这种格式来解码 URL 并从端点获取我需要的数据: 我使用的方法是“GET”,URL 如下所示: 'api/data/serial?from=start time&to=end time&averagingperiod=平均 period&includejournal=包括期刊'
哪里 api/data 是标准的和 /serial 的?是端点的序列号。 (?from=) 和 (&to=) 是我使用ISO 8601 规则设置的参数。 但我需要将时间 (from=) 设置为 24 小时前 (to=) 当前时间,例如: (?from=" . date('Ym-d', $yesterday) . "T" . "00: 00:00" . "&to=" . date("Ymd") . "T" . "00:00:00" .)
日期时间格式如下:(yyyy-mm-ddT00:00:00)
我的代码如下所示:
<?php
// get the timestamp of yesterday
$yesterday = strtotime('-24 Hours');
// get the timestamp of today
$today = date();
// the format we build the date in
$format = 'Y-m-d';
// get the yesterday in the specified format
$from = date($format, $yesterday);
// get today in the specified format
$to = date($format, $today);
// build the URL
$url = 'https://cloud.aeroqual.com/api/data/AQM65%2017112020-804'.'?from='.$from.'T00:00:00'.'&to='.$to.'T00:00:00'.'&averagingperiod=60&includejournal=false'.;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Cookie: .MONOAUTH=C5PBclbeW29kqexXRGTlXaHImDoLhxR/YzALxc44/SZ+KQV2XuMsZps6OJnXMSi56gjRsQ6ED6GIcH7Fk8XCoJ80IrpsTUDPO+Bmf0tVKaPZtIzpwe7ff3QqBKZLFGZK; ASP.NET_SessionId=9F89912F7F571A228B63ACEC; AWSELB=95CB9B4302EC8CCB93BD1C1D5C4630880B3D9B3CCF5F0CE3F60EF45F0604C252A119CAE7D802D28AE24434CB392308307A6E5617B4045026B161BDB6886AF9E5C0388BFFED; AWSELBCORS=95CB9B4302EC8CCB93BD1C1D5C4630880B3D9B3CCF5F0CE3F60EF45F0604C252A119CAE7D802D28AE24434CB392308307A6E5617B4045026B161BDB6886AF9E5C0388BFFED'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
问题:如何格式化我的 URL,以便接收从 24 小时前到现在(当前时间)的数据。 或者如果有人知道,是否可以在 cURL 的 URL 中实现变量。
错误:在我的服务器上运行我的 php 代码时,我收到如下错误:
1:加载资源失败:服务器响应状态为500() 2:VM265:7146 crbug/1173575,非 JS 模块文件已弃用。 (匿名)@ VM265:7146
此代码运行完美,但我想获取从 24 小时前到当前时间的日期和时间值。 :
<?php
$ch = curl_init();
// Configure curl as needed, depending on your application
curl_setopt_array($ch, array(
CURLOPT_URL => "https://cloud.aeroqual.com/api/data/AQM65%2017112020-804?from=2021-1-1T00:00:00&to=2021-2-26T00:00:00&averagingperiod=60&includejournal=false",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Cookie: .MONOAUTH=C5PBclbeW29kqexXRGTlXaHImDoLhxR/YzALxc44/SZ+KQV2XuMsZps6OJnXMSi56gjRsQ6ED6GIcH7Fk8XCoJ80IrpsTUDPO+Bmf0tVKaPZtIzpwe7ff3QqBKZLFGZK; ASP.NET_SessionId=9F89912F7F571A228B63ACEC; AWSELB=95CB9B4302EC8CCB93BD1C1D5C4630880B3D9B3CCF1DD49CB392B355F68787785EB0EA72F7A4B7F495B9BA3840A89055C0FDEAE769045026B161BDB6886AF9E5C0388BFFED; AWSELBCORS=95CB9B4302EC8CCB93BD1C1D5C4630880B3D9B3CCF1DD49CB392B355F68787785EB0EA72F7A4B7F495B9BA3840A89055C0FDEAE769045026B161BDB6886AF9E5C0388BFFED;'
),
));
// Do the request
$response = curl_exec($ch);
// Cleanup
curl_close($ch);
echo $response;
?>
我是一名初级 Web 开发人员,对 Api 不太熟悉,如果我的问题格式不正确,我很抱歉。
【问题讨论】:
-
不太清楚你在这里问什么。可以在您的帖子中添加问题吗?
-
url 目前有什么问题?看起来不错。
-
“日期和时间格式如下:(yyyy-mm-ddT00:00:00)” - 您在示例 URL 中的日期值不会出现将月份设为两位数,但前导零......?
-
$today = date();不是// get the timestamp of today,它会给你一个 error (PHP8),或者至少是一个警告,因为该函数至少需要一个参数。您是说time而不是date吗? -
另外,你的
$url = '…'.;在分号前多了一个点,这也是解析错误。