如何使用php接收从libcurl发送的图像缓冲区?

时间:2018-07-17 11:42:55

标签: php c++ http libcurl

我正在读取图像作为缓冲区,并使用c ++中的libcurl将其发送到php页面。我的问题是我无法在php页面上接收缓冲区。我已经用Wireshark捕获了POST请求。所以我认为问题出在我写的php页面上。

#include <stdio.h>
#include <curl/curl.h>
#include <string>
#include <fstream>  
#include <sstream>  
#include <iostream>
using namespace std;

static const char expectHeader[] = "Expect:";
//static const char jsonHeader[] = "Content-Type:application/json;charset=UTF-8";

static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
    std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
    if (NULL == str || NULL == buffer) {
        return -1;
    }

    char* pData = (char*)buffer;
    str->append(pData, size * nmemb);
    return nmemb;
}


int main(){
    ifstream t("./pic/2.jpg");  //reading image from folder "pic"
    stringstream buffer;  
    buffer << t.rdbuf();  
    string contents(buffer.str());
        t.close();
        cout << "content size:" << contents.size() << endl; 

    //CURL* pCurl;
        curl_global_init(CURL_GLOBAL_ALL);
    int ret = 0;
    string url = "10.5.9.203/website/WebFrontend/index.php";

    struct curl_httppost *post = NULL;
    struct curl_httppost *last = NULL;
        //struct curl_slist *headers =NULL;

        string strResponse;

        curl_formadd(
            &post, &last, CURLFORM_COPYNAME, "pic",
            CURLFORM_BUFFER, "2.jpg", 
            CURLFORM_BUFFERPTR, contents.c_str(), 
            CURLFORM_BUFFERLENGTH, contents.size(),
            CURLFORM_CONTENTTYPE, "multipart/form-data", CURLFORM_END);



         struct curl_slist *plist = NULL;
         plist = curl_slist_append(plist, expectHeader);
         if(NULL == plist)
         {
                cout << "error curl_slist_append";
                return false;
         }

         CURL *curl;
            curl = curl_easy_init();
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
            curl_easy_setopt(curl, CURLOPT_HEADER, 0);
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist);
            curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
            curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
            curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
            curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 1000);
            curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 1000);

            char errbuf[CURL_ERROR_SIZE];
            errbuf[0] = '\0';
            curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);

            CURLcode errCode = curl_easy_perform(curl);
            if(errCode != CURLE_OK)
            {
                ret = false;
                cout << "Verify curl_easy_perform() failed: "
                           << curl_easy_strerror(errCode);

                cout << "url : "
                           << url.c_str();

            }
            curl_easy_cleanup(curl);
            curl_formfree(post);
            curl_slist_free_all(plist);

}

我认为上面的代码可以帮助我发送包含图像缓冲区的帖子请求,但是我不知道我应该如何在php中接收缓冲区。 我写了一个非常简单的php页面试图接收它,但是失败了:

<?php
if(isset($_POST["key"])){ //if the user has logged in
  echo "receive";
  echo "done";
  return;
}
if(isset($_POST["pic"])){ //if the user has logged in
  echo "receive";
  return;
}
if (isset($_FILES["pic"])){
  echo "receive\n";
  print_r($_FILES);
  return;
}

if (isset($GLOBALS)){
  echo "received";
}

if(isset($_POST["name"])){ //if the user has logged in
  echo "receive";
  echo "done";
  return;
}

?>

能帮我如何在php中接收帖子缓冲区吗? 预先感谢。

0 个答案:

没有答案