php curl post请求symfony2应用程序显示一个空的ParameterBag


php curl post request to symfony2 app shows an empty ParameterBag

从php curl请求symfy2应用程序(symfony controller - symfony 'Bundle'FrameworkBundle' controller)

<?php
$string1 = 'foo';
$string2 = 'bar';
$string3 = 'foobar';
$url = 'http://mysitename.com/path/to/symfony2/app';
$myVars = 'var1='.urlencode($string1).'&var2='.urlencode($string2).'&var3='.urlencode($string3);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myVars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);

symfony应用的响应是:

var_dump($request); //Symfony'Component'HttpFoundation'Request
... 
["request"]=>
  object(Symfony'Component'HttpFoundation'ParameterBag)#4 (1) {
    ["parameters":protected]=>
    array(0) {
    }
  }
  ["query"]=>
  object(Symfony'Component'HttpFoundation'ParameterBag)#5 (1) {
    ["parameters":protected]=>
    array(0) {
    }
  }
...

标准PHP文件的响应:

<?php
var_dump($_POST);

:

array(3) {
  ["var1"]=>
  string(3) "foo"
  ["var2"]=>
  string(3) "bar"
  ["var3"]=>
  string(6) "foobar"
}

尝试在配置中将csrf_protection设置为false。Yml没用

当张贴从一个表单,例如http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_submit和改变动作到我的symfony2控制器,我可以得到正确的响应:

... 
  ["request"]=>
  object(Symfony'Component'HttpFoundation'ParameterBag)#4 (1) {
    ["parameters":protected]=>
    array(0) {
    }
  }
  ["query"]=>
  object(Symfony'Component'HttpFoundation'ParameterBag)#5 (1) {
    ["parameters":protected]=>
    array(2) {
      ["FirstName"]=>
      string(6) "Mickey"
      ["LastName"]=>
      string(5) "Mouse"
    }
  }
...

任何帮助都将不胜感激

编辑:

Php -调试Curl

节目:

* About to connect() to host.com port 80 (#38)
*   Trying xxx.xxx.xxx.xxx...
* Connected to host.com (xxx.xxx.xxx.xxx) port 80 (#38)
> POST /path/to/controller HTTP/1.1
User-Agent: Mozila 6.0
Host: host.com
Accept: */*
Referer: http://my/local/env/public
Content-Length: 103
Content-Type: application/x-www-form-urlencoded
* upload completely sent off: 103 out of 103 bytes
< HTTP/1.1 301 Moved Permanently
< Date: Tue, 25 Mar 2014 20:15:25 GMT
< Server: Apache
< Location: http://host.com/path/to/controller
< Vary: Accept-Encoding
< Content-Length: 269
< Connection: close
< Content-Type: text/html; charset=iso-8859-1
< 
* Closing connection 38
* Issue another request to this URL: 'http://host.com/path/to/controller'
* Violate RFC 2616/10.3.2 and switch from POST to GET
* About to connect() to host.com port 80 (#39)
*   Trying xxx.xxx.xxx.xxx...
* Connected to host.com (xxx.xxx.xxx.xxx) port 80 (#39)
> GET /path/to/controller HTTP/1.1
User-Agent: Mozila 6.0
Host: host.com
Accept: */*
Referer: http://my/local/env/public
< HTTP/1.1 500 Internal Server Error
< Date: Tue, 25 Mar 2014 20:15:25 GMT
< Server: Apache
< X-Powered-By: PHP/5.4.26
< Cache-Control: no-cache
< Vary: Accept-Encoding
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
< 
* Closing connection 39

内部服务器错误,因为它无法解析丢失的post数据

你的curl代码看起来对我工作。它也可以向我的应用服务器发送数据。也许错误在别的地方。您的服务器可能在没有UserAgent或Referer的情况下拒绝请求。所以试试这两个:

curl_setopt($ch, CURLOPT_USERAGENT, "Mozila 6.0");
curl_setopt($ch, CURLOPT_REFERER, "http://www.someurl.com/");

同样,以详细模式运行curl。它将帮助您查看curl发送到服务器的请求。与我们分享详细的输出,以便我们可以进一步提供帮助。

curl_setopt($ch, CURLOPT_VERBOSE, 1);

http://evertpot.com/curl-redirect-requestbody/

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTREDIR, 3);

Curl: *违反RFC 2616/10.3.2,从POST切换到GET

答案是设置未记录的CURLOPT_POSTREDIR标志,这样curl就知道在有重定向时发送post数据,还设置CURLOPT_CUSTOMREQUEST标志,这样请求就不会转换为GET请求

设置标志的选项有:

0 -> do not set any behavior
1 -> follow redirect with the same type of request only for 301 redirects.
2 -> follow redirect with the same type of request only for 302 redirects.
3 -> follow redirect with the same type of request both for 301 and 302 redirects.