HTTP POST connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peachsummer
Forum Newbie
Posts: 1
Joined: Fri Jun 11, 2004 7:52 am

HTTP POST connection

Post by peachsummer »

Hello everybody,

What I'm trying to do overall is make a script that will connect to UPS Web Tools and get their shipping rates. I have to send an XML request to UPS and they will send an XML response back to me. What I am having a problem with right now is simply opening the connection to send the request.

The UPS doc says to connect using a secure HTTP connection, POST method, to port 443. Here is the code I have been messing with. This is just the connection code.

Code: Select all

// VALUES:
			// $this->host = "www.ups.com"
			// $this->path = "/ups.app/xml/Rate"
			// $str is the XML request

			// Generate the request header
			$ReqHeader =
			"POST ".$this->path." HTTP/1.1\r\n".
			"Host: ".$this->host."\r\n".
			"Content-Type: application/x-www-form-urlencoded\r\n".
			"Content-Length: ".strlen($str)."\r\n\r\n";
			
			// Open the connection to the host
			$socket = fsockopen("https://".$this->host, 443, $errno, $errstr);
			if (!$socket) {
				$Resultї"errno"] = $errno;
				$Resultї"errstr"] = $errstr;
				return "<b>Connection Error $errno:</b> $errstr<br>\n";
			}
All I'm getting is a failure of the fsockopen function with an $errno of 0. I have tried using "http://", "https://", and "ssl://" in front of $this->host for the connection, and gotten the same result each time.

I don't have much experience with this so I don't know if there's something wrong with the code or the way I'm setting up the function. We don't have cURL installed on our server so I couldn't use cURL functions. (A lot of sample code for UPS rate finders had cURL functions.) I'm beginning to think that something might be wrong with our server that is preventing me from connecting, but I don't know enough about this to know what it would be or how to fix it.

If anyone has experience with this kind of connection, or with using the UPS Web Tools, I would greatly appreciate any advice you might have to offer. Thanks very much!
amolkad
Forum Newbie
Posts: 22
Joined: Tue Jun 01, 2004 7:22 am

HTTP POST connection

Post by amolkad »

Use follwoing code for HTTPS request.

Code: Select all

$book_flight='xml data';
$host = "www.server.com"; 
$port = 8443; 
$path = "/whateverparameter"; 

$fp = fsockopen("ssl://".$host, $port, $errno, $errstr, $timeout = 90); 

if(!$fp){ 
 //error tell us 
 echo "$errstr ($errno)\n"; 
   
}else{ 

  //send the server request 
  fputs($fp, "POST $path HTTP/1.1\r\n"); 
  fputs($fp, "Host: $host\r\n"); 
  fputs($fp, "Content-type: text/xml\r\n"); 
  fputs($fp, "Content-length: ".strlen($book_flight)."\r\n"); 
  fputs($fp, "Connection: close\r\n\r\n"); 
  fputs($fp, $book_flight . "\r\n\r\n"); 

  //loop through the response from the server 
  while(!feof($fp)) { 
   $reply .= fgets($fp, 4096); 
  } 
  //close fp - we are done with it 
  fclose($fp); 
}
edit patrikG: Added

Code: Select all

-tags YET AGAIN. Guys, it's not difficult. Just click on the little button saying PHP when you post things, then insert your code,  then click it again. Woooha! Adventure!
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

The above will work but only if you have PHP 4.3.0 or above AND OpenSSL support compiled directly into it. Just thought I'd mention that as the original poster didn't.
Post Reply