Recently I was working in a hotel booking engine and found a couple of methods to post XML to server; I thought this might be good to share with my friends who want to post xml via HTTP POST method.
There are several ways to Send XML requests via HTTP POST.
I am going to show you two ways. Both are very simple and easy.
As first approach I have used a small xml file with CURL.
'.
''.
'1234567890 '.
'lgsoftwares '.
'mypassword '.
'phpmind.com '.
' '.
''.
' '.
' '.
' '.
''.
'JHM '.
'OGGSHE '.
'101009 '.
'101509 '.
'1 '.
' '.
'';
$URL = "https://www.yourwebserver.com/path/";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
?>
Hi dude, i have also find out one good example
Curl GET And POST Method Calls – PHP Example
Pingback: PHP y cURL - KS7000+WP
Invaluable suggestions ! I loved the details – Does anyone know if I can find a template SSA-632-BK. form to work with ?
Can this code be used with any of the software we are developing or changes need to be done in it?
Hi there to every , for the reason that I am truly keen of reading this website’s post to be updated daily.
It includes good stuff.
This blog post saved me a great deal….am now drinking to that!! Thanks guys
CURLOPT_HTTPHEADER it creates empty output?
WOW just what I was searching for. Came here by searching for POST
Pingback: Women Jordans
Great !
Thank you.
I’m not sure exactly why but this blog is loading incredibly slow for me.
Is anyone else having this issue or is it a issue on my end?
I’ll check back later and see if the problem still exists.
I found the code useful. It worked great but after changing this line curl_setopt($ch, CURLOPT_MUTE, 1); to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
As the provious function was depreciated.
Hi i try the above i got message like
Content-type: text/html
X-Powered-By: PHP/4.3.9
other than that there is nothing my url like http://xxx/xxx.cfm
can any one suggest some solution
thanks for post. code is working fine.
Thanks once again
Good afternoon guys, I tried to implement this example and came across the following message: 411 Length Required.
My example, gotta send via PUT, follows my code below:
$xml_data = ‘
2012-10-23 16:12:50.2 UTC
Observacao
160067
160502
B1CVO
201201000030
160502
1
200055
0181662768
339030
A1DTDEF0000
037448
200.05
160502
1
200055
0181662768
339030
A1DTDEF0000
037448
200.05
‘;
//URL DEC
$url = ‘http://painelsiafihmg.dec.eb.mil.br/ws/service/list/api’;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/xml’));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$output = curl_exec($ch);
var_dump($output);
curl_close($ch);
Thank you so much. I was struggling with this from last couple of days. Today I found your post and it worked in first go. Thanks 🙂 I was also trying to do it with jQuery+Ajax. Can this be done using jquery?
om, You saved my day! Thanks once more for the second time again.
Thanks
I am using RMS Online (http://www.rms.com.au) I had get an error “1 Invalid SOAP username/password. ” though I am puting the original given by the client.
Hi Guys, I’m trying this code but it doesn’t work for me, I need make a PUT request with XML data in body.
I have try this by command line
curl -v -X PUT -d ‘129029Adding notes to URL MMMMMMMM 562000000000’ -H ‘Content-Type: application/xml’ ‘https://service.com/api/’
And works great, but I try this by php and it doesn’t
$xml_data = ‘129029Adding notes to URL MMMMMMMM 3000000’;
$URL = “https://service.com/api/”;
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/xml’));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$output = curl_exec($ch);
var_dump($output);
curl_close($ch);
And this way I’m getting this error:
411 Length Required
What do you think I’m doing wrong.
Thanks a lot
I checked using phpinfo(). CURL 7+ is enabled….
CURL is not active in your case.
http://www.phpmind.com/blog/?p=1048 read this and act!
Thanks
Hi,
I send request in xml format. I can get the response when i execute in localhost and if it is in web server it shows
“cURL Error (7): couldn’t connect to host”
Here’s my code
function xml_post($post_xml, $url)
{
$user_agent = $_SERVER[‘HTTP_USER_AGENT’];
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 50); // times out after 15s
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml); // add POST fields
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
// curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_errno > 0) {
echo “cURL Error ($curl_errno): $curl_error\n”;
} else {
echo “Data received\n”;
}
//print_r($data);
echo $data;
}
?>
What may be the problem?
Hi,
i tried this script but it dont work at all.
i use wampserver 2.2.
wend i execute the scrip by this likn http://localhost/webserviec.client.php it turn a white page !!!!.
i need to send a xml to another server without a form, so i’m searching a php script to do this but i dont find anything.
$postXML = file_get_contents(‘php://input’);
Thanks
You made my DAY 🙂
Hi
It worked for me.
Thanks for such a good tutorial.
hi you can have a look at below link
this has same code.. but they have also explained how to receive it on server end
http://bit.ly/vagu5V
Hi,
I am using this script and its working fine. but I am facing one issue only when the size of xml increases, it throws the following error.
/**************************************************************/
ERROR
The requested URL could not be retrieved
While trying to process the request:
POST /servlet/ExternalServiceRequestServlet HTTP/1.1
Host: qa1.servicemagic.com
Accept: */*
Content-Type: text/xml
Content-Length: 1176
Expect: 100-continue
The following error was encountered:
Invalid Request
Some aspect of the HTTP Request is invalid. Possible problems:
Missing or unknown request method
Missing URL
Missing HTTP Identifier (HTTP/1.0)
Request is too large
Content-Length missing for POST or PUT requests
Illegal character in hostname; underscores are not allowed
/**************************************************************/
Can anyone help me on this.
Thanks in advance.
Hi, I’m Jesica from Argentina, I’m speack English not very well… I want to known how i make for send the request with POST HTTP/1.0 with cURL, I develop a programm that use this tecnology but I don´t known what can i do to send correctly the request.
Thanks a lot…Great it’s really works for me
Thanks for your patient replies once again….
actually my end goal is to post an XML to a site, but what should me my changes in this case?
and yes to view the response i have this file server.php
$xml = file_get_contents(‘client.php’);
$xml = new SimpleXMLElement($xml, LIBXML_NOCDATA, false);
$response = (string) $xml;
$response = strtoupper($response);
echo $response;
your type is xml but you are not posting XML file.
you are not sending XML file. while you are using Content-Type: text/xml
Where you want to send xml file?
ok so i just tried this and when i execute php client.php, it returns
it does not return “Testing” back
???
<?php
$xml = 'Testing’;
$server = ‘server.php’; // URL to server.php
$ch = curl_init($server);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, “$xml”);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo ”, simplexml_load_string($response), ”;
$xml = simplexml_load_string($output);
you should see page source code too.
You can check output using echo $output;
# $output = curl_exec($ch);
yes curl is enabled on my server and how do i check if the any response is coming back ?
First do one thing. Try a to Execute a small CURL code in your server. There are tones of examples over internet and make sure that CURL is enabled in your server. You can check using phpinfo().
Do you know, if there is any response come back in your case when you send XML request using CURL?
can you tell me how i could get the action url to return something on the screen ?
another thing i just noticed, so i thought i might ask you, i have seen my php is becoming very slow and unresponsive while trying out all these curl options? any guesses
i think this will not show you any output until your action url return something. You can check CURL status only if it is true or false by running that code.
Hi om,
Thanks for your reply, i will definatey try to exclude the
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
lines from the file
but i have few more questions that some forums say that the curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
should be after
curl_setopt($ch, CURLOPT_POSTFIELDS, “$xml_data”);
guessing something to do with the XML header part
another questions was more of a concept clearing one…
so suppose i have this name this file as client.php and paste all you script in it (excluding the SSL part) then once i run the script client.php from the webserver should i expect the data to show up in the
http://www.yourwebserver.com/path/
or do i have to script something to be able to receive this data
?
making this a client-server model
thanks again for your response
I have used SSL you don’t have to use if your server does not support. Send your XML normally without SSL. like this. And let me know. In my case i am using SSL so i had given that example and its working for me!! Second thing is check your “CURL lib” with any simple example.
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
curl_setopt($ch, CURLOPT_POSTFIELDS, “$xml_data”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
This does not work for me?
what i have done is copied this file exactly on my webserver and executed it by calling php new2.php
and i am expecting the results to show up in my URL http://www.example.com/post2.php
do i need to add something in the post2.php as well?
very thanks.it’s help me to sending xml with defined port.
Hope you are enjoying other post too!
That code worked great. I forgot how reliable cURL is. I was stuggling sending xml data using fsockopen method but switching to cURL saved the day.