php curl post对symfony2 app的请求显示一个空的ParameterBag

时间:2014-03-25 19:09:10

标签: php symfony redirect post curl

从php curl请求到symfony2应用程序(symfony控制器 - 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"
}

尝试在config.yml中将csrf_protection设置为false无效

从表单发布时,例如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 - Debugging 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

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

2 个答案:

答案 0 :(得分:0)

你的卷曲代码对我有用。它也可以将数据发送到我的应用服务器。可能是错误在其他地方。您的服务器可能在没有UserAgent或Referer的情况下拒绝请求。试试这两个:

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

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

curl_setopt($ch, CURLOPT_VERBOSE, 1);

答案 1 :(得分:0)

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

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

Curl : * Violate RFC 2616/10.3.2 and switch from POST to 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.