Solved

How To Post From A Script

Posted on 2004-03-23
12
255 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
  • 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
 
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now