Hi all. I need to receive via POST an XML.
I have created this hook_menu() entry:
$items['foo'] = array( 'title' => 'Get xml', 'page callback' => 'zb_bar', 'access arguments' => array('access content'), 'type' => MENU_NORMAL_ITEM, );
and this is my function:
function zb_bar() { ob_start(); var_dump($_POST); $mailBody = ob_get_contents();
ob_end_clean(); echo $mailBody; exit;
}
This is my test_script.php:
include_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); $xml = '<?xml version="1.0" encoding="ISO-8859-1"?><req>pippo</req>'; //$header = array('Content-Type' => 'application/x-www-form-urlencoded'); $header = array('Content-Type' => 'text/xml'); $ret = drupal_http_request("http://mysite.it/foo%22,$header,%22POST%22,$xml,3); var_dump($ret);
And this is $ret content:
object(stdClass)#22 (4) { ["request"]=> string(219) "POST /foo HTTP/1.0 Host: www.mysite.it User-Agent: Drupal (+http://drupal.org/) Content-Length: 59 Content-Type: text/html
<?xml version="1.0" encoding="ISO-8859-1"?><req>pippo</req>" ["data"]=> string(13) "array(0) { } " ["headers"]=> array(11) { ["Date"]=> string(29) "Mon, 28 Jun 2010 14:40:46 GMT" ["Server"]=> string(137) "Apache/2.2.9 (Debian) PHP/5.2.13-0.dotdeb.1 with Suhosin-Patch proxy_html/3.0.0 mod_python/3.3.1 Python/2.5.2 mod_perl/2.0.4 Perl/v5.10.0" ["X-Powered-By"]=> string(21) "PHP/5.2.13-0.dotdeb.1" ["Set-Cookie"]=> string(147) "........................................................................................................................" ["Expires"]=> string(29) "Sun, 19 Nov 1978 05:00:00 GMT" ["Last-Modified"]=> string(29) "Mon, 28 Jun 2010 14:40:46 GMT" ["Cache-Control"]=> string(25) "post-check=0, pre-check=0" ["Vary"]=> string(15) "Accept-Encoding" ["Content-Length"]=> string(2) "13" ["Connection"]=> string(5) "close" ["Content-Type"]=> string(24) "text/html; charset=utf-8" } ["code"]=> string(3) "200" }
Why $_POST is empty (see "data" ) ?
M.
Ummm... Looks like you're mixing your metaphores.
To pass variables using CGI, they must be properly encoded key value pairs. Example:
$data = array('xml'=>$xml); $data_string = drupal_query_string_encode($data); drupal_http_request("http://mysite.it/foo%22,$header,%22POST%22,$xml,3);
Alternatively you could use multipart file upload data, but this is how I most often do it when creating rest XML web services.
-----Original Message----- From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Michel Morelli Sent: Monday, June 28, 2010 7:37 AM To: support@drupal.org Subject: [support] How to send a XML to drupal menu callback
Hi all. I need to receive via POST an XML.
I have created this hook_menu() entry:
$items['foo'] = array( 'title' => 'Get xml', 'page callback' => 'zb_bar', 'access arguments' => array('access content'), 'type' => MENU_NORMAL_ITEM, );
and this is my function:
function zb_bar() { ob_start(); var_dump($_POST); $mailBody = ob_get_contents();
ob_end_clean(); echo $mailBody; exit;
}
This is my test_script.php:
include_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); $xml = '<?xml version="1.0" encoding="ISO-8859-1"?><req>pippo</req>'; //$header = array('Content-Type' => 'application/x-www-form-urlencoded'); $header = array('Content-Type' => 'text/xml'); $ret = drupal_http_request("http://mysite.it/foo%22,$header,%22POST%22,$xml,3); var_dump($ret);
And this is $ret content:
object(stdClass)#22 (4) { ["request"]=> string(219) "POST /foo HTTP/1.0 Host: www.mysite.it User-Agent: Drupal (+http://drupal.org/) Content-Length: 59 Content-Type: text/html
<?xml version="1.0" encoding="ISO-8859-1"?><req>pippo</req>" ["data"]=> string(13) "array(0) { } " ["headers"]=> array(11) { ["Date"]=> string(29) "Mon, 28 Jun 2010 14:40:46 GMT" ["Server"]=> string(137) "Apache/2.2.9 (Debian) PHP/5.2.13-0.dotdeb.1 with Suhosin-Patch proxy_html/3.0.0 mod_python/3.3.1 Python/2.5.2 mod_perl/2.0.4 Perl/v5.10.0" ["X-Powered-By"]=> string(21) "PHP/5.2.13-0.dotdeb.1" ["Set-Cookie"]=> string(147) "....................................................................... ................................................." ["Expires"]=> string(29) "Sun, 19 Nov 1978 05:00:00 GMT" ["Last-Modified"]=> string(29) "Mon, 28 Jun 2010 14:40:46 GMT" ["Cache-Control"]=> string(25) "post-check=0, pre-check=0" ["Vary"]=> string(15) "Accept-Encoding" ["Content-Length"]=> string(2) "13" ["Connection"]=> string(5) "close" ["Content-Type"]=> string(24) "text/html; charset=utf-8" } ["code"]=> string(3) "200" }
Why $_POST is empty (see "data" ) ?
M.
Metzler, David ha scritto:
Ummm... Looks like you're mixing your metaphores.
To pass variables using CGI, they must be properly encoded key value pairs. Example:
$data = array('xml'=>$xml); $data_string = drupal_query_string_encode($data); drupal_http_request("http://mysite.it/foo%22,$header,%22POST%22,$xml,3);
Hi, my code works perfectly if i call a web application created by 3rd farm. In Drupal I have the problem that POST is empty.
M.
Hi
The XML will not appear in the post. You need to read the raw stream
See http://www.codediesel.com/php/reading-raw-post-data-in-php/
Gordon
Sent from my iPad
On 30/06/2010, at 5:41 PM, Michel Morelli michel@ziobuddalabs.it wrote:
Metzler, David ha scritto:
Ummm... Looks like you're mixing your metaphores.
To pass variables using CGI, they must be properly encoded key value pairs. Example:
$data = array('xml'=>$xml); $data_string = drupal_query_string_encode($data); drupal_http_request("http://mysite.it/foo%22,$header,%22POST%22,$xml,3);
Hi, my code works perfectly if i call a web application created by 3rd farm. In Drupal I have the problem that POST is empty.
M.
-- Michel 'ZioBudda' Morelli michel@ziobuddalabs.it Sviluppo applicazioni CMS DRUPAL e web dinamiche (LAMP+Ajax) Telefono: 0200619074 Telefono Cell: +39-3939890025 -- Fax: +39-0291390660
http://www.ziobudda.net ICQ: 58351764 http://www.ziobuddalabs.it Skype: zio_budda http://www.ziodrupal.net MSN: michel@ziobuddalabs.it JABBER: michel@ziobuddalabs.it
-- [ Drupal support list | http://lists.drupal.org/ ]
Gordon Heydon ha scritto:
Hi
The XML will not appear in the post. You need to read the raw stream
See http://www.codediesel.com/php/reading-raw-post-data-in-php/
Thanks, this seems to works. I will do more tests.
M.