We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


How To Post From A Script

jimkat asked
Medium Priority
Last Modified: 2010-07-27
I need to post some XML to an external web address (Java servlet on another web server) from within a shopping cart written in php. I need example code for how to do this. I also need to know if it can be done to an SSL address.
Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
whoops, typos! remove the line:

$authstr = ;

it shouldn't be there.

and the last line, $respnse should be $response.

Other than that, it should work, because I am currently using practically the exact same script.
You MAY need to modify this line if the host requires authorization:
$curlheaders[0] = "Authorization: $user . : . $pass";

If that doesn't work, you'll have to figure out the appropriate header the host is expecting on your own.

I forgot to mention that the xml data that you wish to send has to be urlencoded so instead of this:
$requestData = 'some xml text';
so this:
$requestData = urlencode('some xml text');
While I'm around...

$rawpost = fread($fp,9999999999);

just looks prettier by doing:

$rawpost = fread($fp,$_SERVER['CONTENT_LENGTH']);

either one works. the second one is recommended as it is probably more efficient.

This place really needs an edit function, eh Cosmin?


Thanks to both for the help. The curl method looks good but I don't have the module installed. One last thing. I do not want to post as  x-www-form-urlencoded so I changed the content-type to text/xml and it worked fine. However, I am getting all the HTTP response headers back. Can I avoid that and just get the XML response that is coming back to me?

You'll have to strip the headers on your own ... remember that there is a blank line between the headers and the content
"Can I avoid that and just get the XML response that is coming back to me?"

curl curl curl :)

easy to install


Quote:"easy to install" ... that's only if you are on windows, if you are on linux you have to recompile php again
compiling/reinstalling php is pretty darn easy itself as long as you saved the old source tree.

make a php script that just contains <?php phpinfo(); ?>

open it via the web, and it will say something like:
Configure command:
'./configure' '--with-apxs=/usr/local/apache/bin/apxs' '--with-xml' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-magic-quotes' '--with-mysql' '--with-pear' '--enable-sockets' '--enable-track-vars' '--enable-versioning' '--with-zlib'

just add "--with-curl" to the end of that.

cd php-4.3.4
#] rm config.cache
#] make distclean
#] ./configure ......
#] make
#] make install

all done.

hey, if i can do it, anyone with an IQ of 146 can


so you're supposed to have 145 ?? :)
... i didn't say it's hard for me to install php on linux or other os but for someone with no background on linux it can be a little difficult.
I suppose it comes down to a compromise between the user using sockets versus the easier method of using curl but reinstalling php. I don't know jack about linux, really. I just have a test box that I mess around with and have apache set up on. If I mess something up and have to reinstall, then it's no big thang.

VMware would be great for someone without a spare box, but then again it costs more than it would to buy a celeron-based pc.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.