Web Hosting, Design, & Coding forum

General discussion

HTTP Headers and Bad Requests

For the sake of what little hair I have left, I sure hope someone can help me.

I'm trying to integrate the USPS Rate Calculator with a PHP web store. I've got the PHP correctly writing the XML to be passed to the USPS, and if I use that XML in a direct call via browser or HTTP-refresh, I get the correct response from them, so I know I'm doing that bit right. But when I try to use sockets to make the connection so the script can parse the returned XML, I keep getting a BAD REQUEST error. It's got to be somewhere in the HTTP Headers that I'm sending wrong - and I can't find it! I spent 3 hours yesterday banging my head against it, and the USPS help center just replied with "sorry, we don't support our software".

Here's my code, where $file is the name of the USPS API, and $request is the XML encoded data that has to be passed to them:

$fp = fsockopen ("testing.shippingapis.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "ERROR: $errstr ($errno)<br>\n";
} else {
echo "we are calling the request...";
stream_set_timeout($fp, 30);
fwrite ($fp, "GET ".$file." HTTP/1.1\r\nHost: testing.shippingapis.com\r\n\r\n");
fwrite ($fp, $request);
$response = fread($fp, 1024);

echo "<br>responce was: ".$response."<br>";

And what I get back is:
HTTP/1.1 400 Bad Request
Server: Microsoft-IIS/5.0
Date: Fri, 29 Oct 2004 17:57:20 GMT
Connection: close
Content-Type: text/html

All it says is "Bad Request" - it doesn't say WHY, or WHAT BIT is bad.

I've tried it as GET and POST, with and without Content-Type and Content-Length, with the XML as urlencoded and part of the $file string in the Header, I've tried every variation I can think of.



Discussion is locked
You are posting a reply to: HTTP Headers and Bad Requests
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: HTTP Headers and Bad Requests
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.
Collapse -
Re: HTTP Headers and Bad Requests

In reply to: HTTP Headers and Bad Requests

Sorry but I can't answer you question. But if I had to make this happen I would buy a HTTP SNIFFER such as http://www.effetech.com/sniffer/ It has a trial mode so you can see if this will let you dissect what happens on that UPS site so you can fix it.

Hope it helps,


Collapse -
Re: HTTP Headers and Bad Requests

In reply to: HTTP Headers and Bad Requests


I'm hoping you haven't gone bald yet and have figured out the problem. I'm working on a similar application, and am struggeling with same error you are getting. I've had it working for a long time with Visual Basic but I'm struggeling trying to get it to work with PHP.

I'd love to hear your solution if you found one.


Collapse -
Re: HTTP Headers and Bad Requests

In reply to: HTTP Headers and Bad Requests


I struggled with the same error you're getting for a while. Below is a solution I came up with that works for me.

This code uses the POST method. Modify it for GET if you need to.

$url = "http://www.domain.com/dir/processor.asp";

$url = preg_replace("@^http://@i", "", $url);
$host = substr($url, 0, strpos($url, "/"));
$uri = strstr($url, "/");

$formdata = array (
"name" => "Richard Anderson",
"address" => "1600 penn ave",
"cs" => "Washington DC"

while(list($key,$val) = each($formdata)) {

$poststring .= urlencode($key) . "=" . urlencode($val) . "&";


$poststring = substr($poststring, 0, -1);

$fp = fsockopen($host, 80, $errno, $errstr, $timeout = 30);

fputs($fp, "POST $uri HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ".strlen($poststring)."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $poststring . "\r\n\r\n");

// Get the response
while(!feof($fp) and (connection_status()==0)) {
print(fread($fp, 1024*1024));

Popular Forums

Computer Newbies 10,686 discussions
Computer Help 54,365 discussions
Laptops 21,181 discussions
Networking & Wireless 16,313 discussions
Phones 17,137 discussions
Security 31,287 discussions
TVs & Home Theaters 22,101 discussions
Windows 7 8,164 discussions
Windows 10 2,657 discussions


Best Black Friday Deals

CNET editors are busy culling the list and highlighting what we think are the best deals out there this holiday season.