• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

How To Post From A Script

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.
0
jimkat
Asked:
jimkat
  • 6
  • 5
2 Solutions
 
CosminBCommented:
try this code:
<?php
$url = parse_url('http://somesite.com/path');
$requestData = 'some xml text';
$port = 80;
$requestString= "POST ".$url['path']." HTTP/1.1\r\nConnection: close\r\nHost: ".$url['host']."\r\nContent-type: application/x-www.form-urlencoded\r\nContent-length: ".strlen($requestData)."\r\n\r\n".$requestData;
$fp = fsockopen($url['host'], $port, $err_num, $err_msg, 5);
if ($fp)
{
 fputs($fp, $requestString);
 $response='';
 while (!feof($fp))
 {
      $response .= fread($fp, 1024);
 }
 fclose($fp);
}
else
{
  echo 'ERROR :'.$err_msg
}
?>

I used it a while ago to make an XML-RPC application
0
 
BrianPap22Commented:
I recommend the CURL method, as it has built-in SSL capabilities.

Just leave the usessl/user/pass lines commented out if you don't need to use them.

This script assumes the original data is coming in via a standard x-www-form-urlencoded HTML post method.

<?php
$host = "www.example.com";
$path = "/path/to/script";
// $usessl = 1;
// $user = "myuserid";
// $pass = "mypassword";

      $fp = fopen("php://input", "r");
      $rawpost = fread($fp,9999999999);
      fclose($fp);

      $ch = curl_init() or die("Cannot initialize database connection.");
      $authstr = ;
      curl_setopt($ch,CURLOPT_FAILONERROR,1);
      if ($usessl) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
            curl_setopt($ch, CURLOPT_URL, "https://${host}:443" . $path);
      }
      else curl_setopt($ch, CURLOPT_URL, "http://${host}:80" . $path);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      $curlheaders[0] = "Authorization: $user . : . $pass";
      if ($user | $pass) curl_setopt($ch,CURLOPT_HTTPHEADER,$curlheaders);
      curl_setopt($ch,CURLOPT_POST,1);
      curl_setopt($ch,CURLOPT_POSTFIELDS,$rawpost);
      $response=curl_exec($ch);
      $curlerrno = curl_errno($ch);
      curl_close($ch);

      if ($curlerrno) die("Unable to connect to database. Error #$curlerrno.");
      echo $respnse;
?>
0
 
BrianPap22Commented:
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.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CosminBCommented:
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');
0
 
BrianPap22Commented:
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?
0
 
jimkatAuthor Commented:
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?
0
 
CosminBCommented:
You'll have to strip the headers on your own ... remember that there is a blank line between the headers and the content
0
 
BrianPap22Commented:
"Can I avoid that and just get the XML response that is coming back to me?"

curl curl curl :)

easy to install

http://www.php.net/manual/en/ref.curl.php
0
 
CosminBCommented:
Quote:"easy to install" ... that's only if you are on windows, if you are on linux you have to recompile php again
0
 
BrianPap22Commented:
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

:)
0
 
CosminBCommented:
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.
0
 
BrianPap22Commented:
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.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now