【发布时间】:2021-01-04 11:33:19
【问题描述】:
数据没有到达。 这是RaspberryPI上的c程序:
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://iautos.be/TestMake.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "d=2020/09/16 16:00:00,21.6,P,");
/* url could be redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
网络服务器上的目标 PHP 文件, 这在浏览器(Firefox)中运行良好: http://iautos.be/TestMake.php?d=2020/09/14%2004:00:00,21.6,P,
<?php
// Program to display URL of current page.
// Put the host(domain name, ip) to the URL.
$link = $_SERVER['HTTP_HOST'];
// Append the requested resource location to the URL
$link .= $_SERVER['REQUEST_URI'];
echo 'Url: ' , $link , "<br/>\n";
$Data = array();
$Data = explode(',', trim($_GET['d']) );
for ($x = 0; $x <= 2; $x++) {
echo 'Data' , $x , ': ' , $Data[$x] , "<br/>\n" ;
}
print "Success.\n";
?>
问题: 当我使用 Firefox 发布数据时,它运行良好。您可以对其进行测试(上面的链接)。 使用 libcurl 不会发送数据。 有什么问题?
这是 RaspberryPI 上 libcurl 测试程序的输出:
Url: iautos.be/TestMake.php<br/>
Data0: <br/>
Data1: <br/>
Data2: <br/>
Success.
这是 Firefox 的输出:
Url: iautos.be/TestMake.php?d=2020/09/14%2004:00:00,21.6,P,
Data0: 2020/09/14 04:00:00
Data1: 21.6
Data2: P
Success.
【问题讨论】:
-
您正在设置
CURLOPT_POSTFIELDS,但页面似乎需要GET变量,而不是POST变量。你试过curl_easy_setopt(curl, CURLOPT_URL, "http://iautos.be/TestMake.php?d=2020/09/14%2004:00:00,21.6,P,");吗?
标签: php curl gcc raspberry-pi libcurl