未接收到php curl post数据

未接收到php curl post数据,php,curl,Php,Curl,下面的代码将数据发送到一个测试页面,该页面只是var_转储了_请求变量。该页面没有获取post参数,而是获取get参数。为什么会发生这种情况 <?php $jsonData = '{"Page":"index.html","KeyNumber":12132321}'; $timeout = 20; $options = array( CURLOPT_URL => "http://myadmin/postdump.php?AID=100&a

下面的代码将数据发送到一个测试页面,该页面只是var_转储了_请求变量。该页面没有获取post参数,而是获取get参数。为什么会发生这种情况

<?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;
?>  

手册页中介绍了这些选项。为
CURLOPT_POSTFIELDS
提供的信息如下:

要在HTTP“post”操作中发布的完整数据。要发布文件, 在文件名前加@并使用完整路径。文件类型可以是 通过在文件名后面加上 格式';类型=mimetype'此参数可以作为 URL编码的字符串,如“para1=val1¶2=val2&…”或作为带有 字段名作为键,字段数据作为值。如果值是数组, 内容类型标题将设置为多部分/表单数据。从PHP开始 5.2.0,如果文件以@前缀传递到此选项,则值必须是数组

所以我想不是这个:

CURLOPT_POSTFIELDS =>'jsonData='.$jsonData,
。。。你想要这样的东西:

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

尝试使用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'
   );
最近,我还在下面附带的curl实用程序类中工作,以帮助测试一些表单,希望这能有所帮助:

<?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;
        }
    }
}

?>


您是否在没有任何准备的情况下尝试过?另外,您是否尝试过使用
var\u dump
ing
$\u POST
?我认为如果您将字段作为字符串传递,则必须手动
urlcode
对值进行编码……手册中说
CURLOPT\u POSTFIELDS
必须是urlcoded字符串或数组。你试过用URL编码那个jsonData吗?