Solved

How To Post From A Script

Posted on 2004-03-23
12
272 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

617 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