【问题标题】:Post data with libcurl to PHP $_Get使用 libcurl 将数据发布到 PHP $_Get
【发布时间】: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


【解决方案1】:

CURL 中的 POSTFIELDS 作为 POST 发送到服务器,因此这些参数将位于 $_POST 超全局数组中。

【讨论】:

    【解决方案2】:

    抱歉,我是这方面的新手。 我又做了一些测试。这有效:

    curl_easy_setopt(curl, CURLOPT_URL, "http://iautos.be/TestMake.php/?d=2020/09/16%2016:00:00,21.6,P,");
    

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 2012-07-20
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多