2012-06-14 115 views
0

下面的代碼發送數據到一個測試頁面,只是var_dumps _REQUEST變量。頁面沒有得到post參數,但獲取get參數。爲什麼發生這種情況?php curl post數據不被接收

<?php 
    $jsonData = '{"Page":"index.html","KeyNumber":12132321}'; 
    $timeout = 20; 
    $options = array(
     CURLOPT_URL => "http://myadmin/postdump.php?AID=100&age=5&ishide=0", 
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_CONNECTTIMEOUT => $timeout, 
     CURLOPT_POST => true, 
     CURLOPT_POSTFIELDS =>'jsonData='.$jsonData, 
     CURLOPT_ENCODING=>"", 
     CURLOPT_FOLLOWLOCATION=>true 
     ); 

    $ch = curl_init(); 
    curl_setopt_array($ch, $options); 
    $file_contents = curl_exec($ch); 
    curl_close($ch); 

    echo $file_contents; 
?> 
+0

您是否嘗試過它沒有在得到什麼?另外,你是否嘗試過''_POST'的'var_dump'? – gcochard

+0

我認爲如果您將字段作爲字符串傳遞,您必須手動'urlencode'的值... – prodigitalson

+1

該手冊說''CURLOPT_POSTFIELDS'必須是urlencoded字符串或數組。你有沒有試過urlencoding那個jsonData? –

回答

0

這些選項在curl_setopt手冊頁中進行了說明。 CURLOPT_POSTFIELDS提供的信息是這樣的:

要在HTTP「POST」操作中發佈的完整數據。要發佈文件, 需要在文件名前添加@並使用完整路徑。文件類型可以是 ,其格式爲: 格式'; type = mimetype'。 此參數可以作爲 urlencoded字符串傳遞,如'para1 = val1 & para2 = val2 & ...'或作爲數組與 字段名稱作爲鍵和字段數據作爲值。如果value是一個數組,則 的Content-Type標題將被設置爲multipart/form-data。從PHP 開始5.2.0,如果使用@前綴將文件傳遞給此選項,則值必須是數組。

所以我想,與其這樣:

CURLOPT_POSTFIELDS =>'jsonData='.$jsonData, 

...你想是這樣的:

CURLOPT_POSTFIELDS => array('jsonData' => $jsonData), 
0

嘗試使用進行urlencode函數來編碼字段的值和我還建議將請求頭添加到請求中,以模擬將包含頭的數組傳遞給CURLOPT_HTTPHEADER選項的表單請求,例如

$headers = array(
     'Host: domain.com', 
     'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
     'Accept-Encoding: gzip, deflate', 
     'Content-Type: application/x-www-form-urlencoded' 
    ); 

最近還我在下面附着在測試某種形式的幫助捲曲實用類的工作,希望這有助於:

<?php 

define('CURLMANAGER_USERAGENT_MOZ4', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); 
/** 
* 
* Makes remote calls via http protocol 
* @author Alejandro Soto Gonzalez 
* 
*/ 
class CURLManager { 
    private $fields = array(); 
    private $headers = false; 
    private $method = ''; 
    private $url = ''; 
    private $response = ''; 
    private $header = false; 

    /** 
    * 
    * Constructor 
    * @return void 
    */ 
    public function __construct() { 

    } 

    /** 
    * 
    * Executes the http request 
    * 
    * @param string $useragent Request user agent 
    * @param string $fields Post values to be sent in the request body 
    * @param array $headers Request headers 
    * @param string $method Http method POST, GET, etc.. 
    * @param string $url Remote url 
    * @param boolean $header true if the response should contain the http headers and false if not 
    * @return void 
    */ 
    public function executeRequest($useragent, $fields, $headers, $method = 'POST', $url, $header = false) { 
     $this->fields = $fields; 
     $this->method = $method; 
     $this->url = $url; 
     $this->headers = $headers; 
     $this->header = $header; 
     $this->initializeCURL($useragent); 
    } 

    /** 
    * 
    * Gets the response retrieved from the http request 
    * @return void 
    */ 
    public function getResponse() { 
     return $this->response; 
    } 

    /** 
    * 
    * Initializes curl and executes the request 
    * @param string $useragent Request user agent 
    * @return void 
    */ 
    private function initializeCURL($useragent) { 
     $ch = curl_init($this->url); 
     $this->addFieldsToRequestBody($ch, $this->method); 
     $this->addGenericOptions($ch, $useragent); 
     $this->showResponseHeaders($ch, $this->header); 
     $this->addRequestHeaders($ch, $this->headers); 
     $this->response = curl_exec($ch); 
     curl_close($ch); 
    } 

    /** 
    * 
    * Adds generics options to the current curl object 
    * @param curlObject $ch 
    * @param string $useragent Request user agent 
    * @return void 
    */ 
    private function addGenericOptions($ch, $useragent) { 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
     curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/sourceforge/bouncer-logger-tester/cookies.txt'); 
    } 

    /** 
    * 
    * Adds the data fields to request body 
    * @param curlObject $ch 
    * @param string $method Http method POST, GET, etc.. 
    * @return void 
    */ 
    private function addFieldsToRequestBody($ch, $method) { 
     if ($this->method=='POST') { 
      curl_setopt($ch, $this->getCurlMethod(), true); 
      curl_setopt($ch, $this->getCurlFieldsMethod(), $this->fields); 
     } 
    } 

    /** 
    * 
    * Adds headers to the current request 
    * @param curlObject $ch 
    * @param array $headers Array containing the http header 
    */ 
    private function addRequestHeaders($ch, $headers) { 
     var_dump($headers); 
     if ($headers !== false) { 
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     } 
    } 

    /** 
    * 
    * Gets the curl method id 
    * @return integer 
    */ 
    private function getCurlMethod() { 
     switch ($this->method) { 
      case 'POST': return CURLOPT_POST; 
      default: return CURLOPT_POST; 
     } 
    } 

    /** 
    * 
    * Show response headers in the full text response 
    * @param curlObject $ch 
    * @param boolean $show True to show headers and false to not 
    * @return void 
    */ 
    private function showResponseHeaders($ch, $show) { 
     if ($this->header) { 
      curl_setopt($ch, CURLOPT_HEADER, 1); 
     } 
    } 

    /** 
    * 
    * Gets curl fields option 
    * @return void 
    */ 
    private function getCurlFieldsMethod() { 
     switch ($this->method) { 
      case 'POST': return CURLOPT_POSTFIELDS; 
      default: return CURLOPT_POSTFIELDS; 
     } 
    } 
} 

?>