0

我正在使用 libcurl 向我的 Web 服务器上的 index.php 文件发出简单的 http 发布请求,该文件具有以下简单的代码将发布数据写入文件

if (isset($_POST['abc'])){
    $log="abc.log";
    $h=fopen($log,"w");
    fwrite($h,$_POST['abc']);
    fclose($h);
}  

而我编写的实现http post请求的c++代码如下

    CURL *curl;
    CURLcode res;
    curl=curl_easy_init();
    string url=AnsiString("http://127.0.0.1/curl/index.php?abc=123").c_str();
    string data="abc=3434";
    if (curl) {

        curl_easy_setopt(curl, CURLOPT_POST, 1);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS,AnsiString("abc=3434").c_str());
        curl_easy_setopt(curl, CURLOPT_URL,AnsiString("http://127.0.0.1/curl/index.php").c_str());
        res=curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }

首先,启用发布curl_easy_setopt(curl, CURLOPT_POST, 1);

其次,如果我调试此代码,我会CURL_OK在(返回值)中获得状态,res并且每次我运行代码监控时都会建立 TCP 127.0.1:80,netstat -ano 15但文件abc.log为空

这有什么问题??你没有任何想法指出我正确的方式吗?

先感谢您!

4

1 回答 1

0

发送有CURLOPT_POSTFIELDSIZE帮助!
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,data.Length());

Apache 日志说你已经发送 POST 没问题

127.0.0.1 - - [03/Mar/2012:17:39:06 +0500] "POST /curl/post.php HTTP/1.1" 200 -

于 2012-03-03T12:46:22.003 回答