2013-07-14 40 views
0

很基本的問題,但我不知道還有什麼要做。libcurl發送帖子到服務器上的php(使用POST在線服務),服務器什麼都看不到

從碼處理我的捲曲請求,並執行它:

ResponseData Request::exec() 
{ 

    CURLcode res = CURL_LAST; 
    CURL *h = curl_easy_init(); 
    ResponseData response; 

    if(h) { 
     curl_easy_setopt(h, CURLOPT_URL, getURL().c_str()); 
     curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, handle_data); 

     switch(this->_type) { 
     default: 
     case TYPE_GET: 
      printf("sending get"); 
      break; 
     case TYPE_POST: 
      printf("sending post"); 
      curl_easy_setopt(h, CURLOPT_POST, 1); 
      break; 
     case TYPE_PUT: 
      printf("sending put"); 
      curl_easy_setopt(h, CURLOPT_PUT, 1); 
      break; 
     case TYPE_DELETE: 
      curl_easy_setopt(h, CURLOPT_CUSTOMREQUEST, "DELETE"); 
      break; 
     } 

     //printf("%d", postData.size()); 
     if(postData.size() > 0) { 
      string post; 
      int it = 0; 
      for each (postDataItem var in postData) 
      { 
       it++; 
       //printf("\n[]Key: %s, Value: %s", var.key, var.value); 
       post.append(var.key); 
       post.append("="); 
       post.append(var.value); 

       if(it != postData.size()) { 
        // dont append & if last element 
        post.append("&"); 
       } 
      } 



      const char * postString = post.c_str(); 
      const char * postUrl = curl_easy_escape(h, postString, 0); 

      printf("\npayload: %s\n\n", postUrl); 

      curl_easy_setopt(h, CURLOPT_POSTFIELDS, postUrl); 
     } 

     res = curl_easy_perform(h); 

     switch(res) 
     { 
     case CURLE_WRITE_ERROR: 
      break; 
     case CURLE_OK: 
      response.setData(contents.c_str()); 
      break; 
     } 

     curl_easy_cleanup(h); 

     return response; 

    } else { 
     printf("Could not initialize curl"); 
    } 
} 

,即時通訊目前作爲POSTFIELDS發送的數據是:

username%3DDuke%26password%3DNukem 

,當我使用Wireshark看到什麼怎麼回事

Message: POST /api/account/login HTTP/1.1 
Request Method: POST 
Request URI: /api/account/login 
Request Version: HTTP/1.1 
Host: [url redacted, not important] 
Line-based text data: application/x-www-form-urlencoded 
    username%3DDuke%26password%3DNukem 

所以,據我所知,我發送這將是一個正常的POST FR例如,一個HTML表單,對吧?

在該URL服務器端,是一個基本沒有邏輯的PHP腳本,看起來像這樣:

<? 
print("GET"); 
var_dump($_GET); 
print("POST"); 
var_dump($_POST); 
print("REQUEST"); 
var_dump($_REQUEST); 

我在我的控制檯輸出看?

GETarray(0) { 
} 
POSTarray(0) { 
} 
REQUESTarray(0) { 
} 

洙......林不知道什麼即時做錯了....是我的C++捲曲請求缺少的東西發送給服務器是否正常?

回答

0

是一個簡單的範圍問題!

兩種方式來解決這個問題:

相反的:

if(postData.size() > 0) { 
    string post; 
    int it = 0; 

我需要:

if(postData.size() > 0) { 
     static string post; 
     int it = 0; 

或聲明string post;以下ResponseData response;