Solved

How To Post From A Script

Posted on 2004-03-23
12
271 Views
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.
0
Comment
Question by:jimkat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 6

Accepted Solution

by:
CosminB earned 400 total points
ID: 10665464
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
 
LVL 4

Assisted Solution

by:BrianPap22
BrianPap22 earned 100 total points
ID: 10667406
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
 
LVL 4

Expert Comment

by:BrianPap22
ID: 10667453
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 6

Expert Comment

by:CosminB
ID: 10667465
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
 
LVL 4

Expert Comment

by:BrianPap22
ID: 10669500
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
 

Author Comment

by:jimkat
ID: 10674523
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
 
LVL 6

Expert Comment

by:CosminB
ID: 10674635
You'll have to strip the headers on your own ... remember that there is a blank line between the headers and the content
0
 
LVL 4

Expert Comment

by:BrianPap22
ID: 10674818
"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
 
LVL 6

Expert Comment

by:CosminB
ID: 10674835
Quote:"easy to install" ... that's only if you are on windows, if you are on linux you have to recompile php again
0
 
LVL 4

Expert Comment

by:BrianPap22
ID: 10674938
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
 
LVL 6

Expert Comment

by:CosminB
ID: 10675014
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
 
LVL 4

Expert Comment

by:BrianPap22
ID: 10675036
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question