Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 7th, 2004, 09:58 PM
Registered User
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to karateka Send a message via Yahoo to karateka
Default UPS servlet interaction with XML via PHP fsockopen

So here's the deal. I'm trying to write a function that contacts a UPS servlet and sends it two sets of XML data via the HTTPS POST method.

Having no experience in both XML and manually creating HTTP(S) headers, I cut and pasted a http_post function that I found via our friends at google.com

With a little modification, it looks like this:
Code:
function http_post($server, $port, $url, $data) {
// example:
//  http_post(
//    "www.fat.com",
//    80, 
//    "/weightloss.pl", 
//    array("name" => "obese bob", "age" => "20")
//   or XML data
//    );
// $data can be a string or an array
//

  //$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
  $user_agent = $_SERVER['HTTP_USER_AGENT'];
  $urlencoded = "";
  $fp = NULL;

  if ( is_array($data) ) {
    while (list($key,$value) = each($data))
      $urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
    $urlencoded = substr($urlencoded,0,-1);    
  }
  else {
    $urlencoded = $data;
  }

  $content_length = strlen($urlencoded);
  $headers = "POST $url HTTPS/1.1
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: $content_length

";

  $fp = fsockopen($server, $port, $errno, $errstr, 15);

  if (!$fp) {
    return false;
  }

  fputs($fp, $headers);
  fputs($fp, $urlencoded);

  $ret = "";
  while ( !feof($fp) ) {
    $ret .= fgets($fp, 1024);
  }

  var_dump( stream_get_meta_data($fp) );

  return $ret;
}
The call to var_dump gives me
Code:
array(5) {
  ["stream_type"]=>  string(6) "socket"
  ["unread_bytes"]=>  int(0)
  ["timed_out"]=>  bool(false)
  ["blocked"]=>  bool(true)
  ["eof"]=>  bool(true)
}
string(147) "
Bad request

Your browser sent a query this server could not understand."
The data that I am sending with the HTTPS POST was provided from UPS and I simply copy and pasted it
Code:
$access_request=<<<EOF
<?xml version="1.0"?>
<AccessRequest xml:lang="en-US">
   <AccessLicenseNumber>TEST262223144CAT</AccessLicenseNumber>
   <UserId>Justin</UserId>
   <Password>password</Password>
</AccessRequest>
EOF;
But no matter what I do (which is very little from my lack off experience) I still get the "Bad request" error page. The UPS serlvet API documentation says that if an authentication error occurs or there is a general error with the posted XML, it will reply with XML containing the occured error(s).

So basically, does anything look wrong with the code that I have so far? I'm off to test the posting functionality on some of my scripts.

http://gha.booleangate.org
http://www.booleangate.org
 
Old September 7th, 2004, 10:00 PM
Registered User
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to karateka Send a message via Yahoo to karateka
Default

I tested the function using two files: post.php and dump.php and I get the output that I expected:
Code:
array(5) {
  ["stream_type"]=>
  string(6) "socket"
  ["unread_bytes"]=>
  int(0)
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(true)
}
string(223) "HTTP/1.1 200 OK
Date: Tue, 07 Sep 2004 17:48:10 GMT
Server: Apache/1.3.29 (Darwin) PHP/4.3.4
X-Powered-By: PHP/4.3.4
Connection: close
Content-Type: text/html


array(1) {
  ["test"]=>
  string(4) "asdf"
}

"
I also updated the http_post function to look like this
Code:
function http_post($server, $port, $url, $data, $protocol="HTTP/1.0") {
// example:
//  http_post(
//    "www.fat.com",
//    80, 
//    "/weightloss.pl", 
//    array("name" => "obese bob", "age" => "20")
//   or XML data
//    );
// $data can be a string or an array
//

    //$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
  $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $urlencoded = "";
  $fp = NULL;

  if ( is_array($data) ) {
    while (list($key,$value) = each($data))
      $urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
    $urlencoded = substr($urlencoded,0,-1);    
  }
  else {
    $urlencoded = $data;
  }

    $content_length = strlen($urlencoded);
    $headers = "POST $url $protocol
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: $content_length

";

  $fp = fsockopen($server, $port, $errno, $errstr, 15);

    if (!$fp) {
        return false;
    }

    fputs($fp, $headers);
    fputs($fp, $urlencoded);

    $ret = "";
    while ( !feof($fp) ) {
        $ret .= fgets($fp, 1024);
  }

  var_dump( stream_get_meta_data($fp) );

    return $ret;
}
Notice that I added the $protocol argument to the function so HTTP and HTTPS request can be made easily.

post.php looks like this
Code:
<pre><?php

require_once($_SERVER['DOCUMENT_ROOT']."/inc/php/net.php");


$data = array("test" => "asdf");

var_dump(
 http_post("127.0.0.1", 80, "/test/dump.php", $data)
);


?></pre\>
And dump.php looks like this
Code:
<pre><?php

var_dump($_REQUEST);

?></pre>
I tried posting the XML to UPS using HTTP but it just redirected me :(

http://gha.booleangate.org
http://www.booleangate.org
 
Old October 20th, 2004, 12:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have you tried playing with the cURL extension? It makes posting data via http/https pretty easy.
  http://www.php.net/curl


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using XPath to get a certain field from XML (UPS) taleriana XML 4 May 24th, 2005 08:00 AM
fsockopen ssl failure: why? donh BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 3 September 16th, 2004 05:21 PM
Get Servlet without mapping in web.xml Abhijitsaha2000 Servlets 1 August 9th, 2004 04:17 AM
servlet example-Wrox Professional JAVA XML sarahmapg Servlets 1 August 5th, 2004 12:33 AM
Servlet with XML benze83 XML 2 July 25th, 2003 03:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.