简体   繁体   English

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

[英]php curl post request to symfony2 app shows an empty ParameterBag

from a php curl request to a symfony2 app (symfony controller - Symfony\\Bundle\\FrameworkBundle\\Controller) 从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);

The response on the symfony app is: 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) {
    }
  }
...

the response from a standard php file: 来自标准php文件的响应:

<?php
var_dump($_POST);

is: 是:

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

tried setting csrf_protection to false in config.yml to no avail 尝试在config.yml中将csrf_protection设置为false无济于事

when posting from a form eg http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_submit and changing the action to my symfony2 controller i can get the correct response: 当从表单发布时,例如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"
    }
  }
...

any help will be appreciated 任何帮助将不胜感激

edit: 编辑:

Php - Debugging Curl PHP-调试卷曲

shows: 说明:

* 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

internal server error is expected as it fails to parse the missing post data 内部服务器出现错误,因为它无法解析缺少的帖子数据

Your curl code look working to me. 您的卷曲代码对我来说很有效。 It can send data to my application server as well. 它也可以将数据发送到我的应用服务器。 May be the error is at other place. 可能是错误在其他地方。 Your server may be rejecting request without UserAgent, or Referer may be. 您的服务器可能在没有UserAgent的情况下拒绝了请求,或者可能是Referer。 So try this two: 因此,请尝试以下两个:

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

Also, run your curl with verbose mode. 另外,使用详细模式运行卷曲。 It will help you to see the requests that curl sending to your server. 它将帮助您查看curl发送到服务器的请求。 Share that verbose output with us, so that we can help further. 与我们分享该详细输出,以便我们提供进一步的帮助。

curl_setopt($ch, CURLOPT_VERBOSE, 1);

http://evertpot.com/curl-redirect-requestbody/ 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 卷曲:*违反RFC 2616 / 10.3.2并从POST切换到GET

the answer is to set the undocumented CURLOPT_POSTREDIR flag so curl knows to send post data when there is a redirect, also to set the CURLOPT_CUSTOMREQUEST flag so the request is not converted into a GET request 答案是设置未记录的CURLOPT_POSTREDIR标志,以便curl在存在重定向时知道发送帖子数据,还可以设置CURLOPT_CUSTOMREQUEST标志,以便将请求不转换为GET请求

options for setting the flag are: 设置标志的选项是:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM