2

from a php curl request to a symfony2 app (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);

The response on the symfony app is:

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

when posting from a form e.g. 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:

... 
  ["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

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

Community
  • 1
  • 1
  • Take a look at this [http://stackoverflow.com/questions/22000720/connect-to-a-symfony-application-through-curl] and ask me if you have question – Javad Mar 25 '14 at 20:30
  • Thanks Javad, there is no security implemented for this controller. The request gets through fine, the problem is picking up the post parameters from the `Symfony\Component\HttpFoundation\Request` object. – pandemicGander Mar 25 '14 at 20:41
  • Put the post params in array then use `http_build_query` to convert the posted data correctly to real params then send it to `CURLOPT_POSTFIELDS` – Javad Mar 25 '14 at 20:44
  • Thanks Javed, I could use http_build_query but I'm doing it myself i.e. `$myVars = 'var1='.urlencode($string1).'&var2='.urlencode($string2).'&var3='.urlencode($string3);` this is what the function http_build_query(...) does with an array. http://us3.php.net/manual/en/function.http-build-query.php – pandemicGander Mar 25 '14 at 20:54

2 Answers2

0

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. 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. Share that verbose output with us, so that we can help further.

curl_setopt($ch, CURLOPT_VERBOSE, 1);
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • Thanks Sabuj, but unfortunately i get the same empty response in the parameter bag. `["request"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#4 (1) { ["parameters":protected]=> array(0) { } } ["query"]=> object(Symfony\Component\HttpFoundation\ParameterBag)#5 (1) { ["parameters":protected]=> array(0) { } }` and `ob_start(); var_dump(curl_exec($ch)); $result = ob_get_clean(); exit($result);` just returns the html response and doesn't show any curl debug info. potentially: https://bugs.php.net/bug.php?id=65348 – pandemicGander Mar 25 '14 at 19:49
  • 1
    Just realised i have to check php error logs for this info: http://stackoverflow.com/questions/3757071/php-debugging-curl will let you know what i find – pandemicGander Mar 25 '14 at 20:07
  • I've updated my question instead of this comment for clarity of showing the curl debug information – pandemicGander Mar 25 '14 at 20:22
  • Your server is redirecting the curl after the request `HTTP/1.1 301 Moved Permanently` and somehow curl gets error with it. May be you are running an older version of curl! You can check from your server about why its redirecting your request. By the way, your `curl` code is ok. You have nothing to do from curl's side. – Sabuj Hassan Mar 25 '14 at 20:38
  • How can you send a CURL request to the Symfony app while the sessionID is not set in the header; as you know `require_previous_session` is true by default in Symfony – Javad Mar 25 '14 at 20:40
  • Thanks for your help Sabuj, it was the redirect causing the issue. I've posted the solution in my question. – pandemicGander Mar 25 '14 at 22:47
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

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

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.
Community
  • 1
  • 1