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