Page 1 of 1

sending HTTP requests

Posted: Tue Jan 07, 2003 11:35 pm
by Cruiser
Ok, I've been busting my head over this for the past couple days, and was hoping someone on this forum could help me out here.

This is the first time i've attempted to code something to send and recieve an http request automatically, and was wondering if there was something in my code that is fundamentally wrong.

I am invoking a service online, sending it an xml document, and it is supposed to respond with one. The only response i get is an http response statying status 100, which means continue, so obviously something passed. Then I get another http response right after that giving me a 400 error, which means its not recognizing my request (problem in the header i'm sending.) Here is a look at my code.

Code: Select all

<?php
$holdmessage = 'xmlrequest';
$messagesize = strlen($holdmessage);
$query = "POST /ShippingAPITest.dll HTTP/1.1\r\n".
               "Host: testing.shippingapis.com:80:\r\n".
               "Content-Type: application/x-www-form-urlencoded\r\n".
               "Content-Length: ".$messagesize."\r\n\r\n";

$fp = fsockopen("testing.shippingapis.com",80);
if (!$fp)
  echo "FAIL!";
else &#123;
  $len = strlen($query);
  fputs($fp, $query, $len);
  fputs($fp, $holdmessage, $messagesize)
&#125;

echo "<html><body><form>";
echo "<textarea name='resultmessage' rows='30' nowarp cols='80'>";
echo fpassthru($fp);
echo "</textarea></form></body></html>";
?>
&#125;
so that is my code, and it puts into the text box the response. I've already explained the response i'm getting, and it has to do with something being wrong with my HTTP header i'm sending, but i'm not sure what. Maybe someone here can shed some light on what i'm doing wrong. Would be greatly appreciated.

Posted: Wed Jan 08, 2003 2:30 am
by volka
hmmmm. not quite sure, but since you're using http 1.1 you should read http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html very carefully or switch to http 1.0 ;)

e.g.
HTTP/1.1 applications that do not support persistent connections MUST include the "close" connection option in every message.

Posted: Mon Aug 11, 2003 9:08 am
by zyrill
another problem with sending post data... :evil:

Code: Select all

<?php
$formdata=array(
        "s" => "59321c93be2f643991483463a9e1ad28",
        "action" => "login",
        "username" => "TestUser",
        "password" => "TestPass");
foreach($formdata as $key => $val){
  $poststring.=urlencode($key)."=".urlencode($val)."&";
}
$poststring=substr($poststring, 0, -1);
$urlHandle=fsockopen("149.232.182.200",80,$errno,$errstr);
if(!$urlHandle){echo "Unable to perform an fsockopen. Error occured: ($errno) [$errstr] - Debuginfo: (urlHandle): [$urlHandle]";}
else{
  socket_set_timeout($urlHandle,10);
  $urlString="POST http://149.232.182.200/try.php HTTP/1.1\r\nHost: 149.232.182.200\r\nUser-Agent: Mozilla\r\nProxy-Authorization: Basic cGhpbGlwcC5rcnVlZ2VyOjEyMzQ1\r\n\r\n";
  $urlString.="Content-length: ".strlen($poststring)."\r\nContent-Type: application/x-www-form-urlencoded\r\n$poststring\r\nConnection: close\r\n\r\n";
  fputs($urlHandle,$urlString);
  while(!feof($urlHandle)){
    $buffer.=fgets($urlHandle);
  }
  echo $urlString;
#  echo $buffer;
  fclose($urlHandle);
?>
when i echo $buffer it keeps telling me: "Bad Request" - the header i'm sending is:

Code: Select all

POST http://149.232.182.200/try.php HTTP/1.1
Host: 149.232.182.200
Connection: Keep-Alive
User-Agent: Mozilla
Proxy-Authorization: Basic cGhpbGlwcC5rcnVlZ2VyOjEyMzQ1

Content-length: 83
Content-Type: application/x-www-form-urlencoded
s=59321c93be2f643991483463a9e1ad28&amp;action=login&amp;username=TestUser&amp;password=TestPass
i'm desperate... i combed the stupid http-docs and specs but without any results... anyone - help, PLEASE!

Posted: Mon Aug 11, 2003 9:58 am
by hedge
I wonder if you need to send a connection close header. I have some wrapper functions for doing things like this that seem to work. Maybe you should check them out.

start here and follow the links viewtopic.php?t=11560

Posted: Mon Aug 11, 2003 4:21 pm
by Seth_[php.pl]
Try HTTP/1.0 instead of HTTP/1.1

Posted: Tue Aug 12, 2003 12:56 am
by zyrill
thanks... strangly enough, that did it... i don't understand why though!
so here's the code that works:

Code: Select all

$formdata=array(
        "s" => "$sessionhash",
        "action" => "login",
        "username" => "USERTEST",
        "password" => "USERPASS");
$poststring="";
foreach($formdata as $key => $val){
  $poststring.=urlencode($key)."=".urlencode($val)."&";
}
$poststring=substr($poststring, 0, -1);
$urlHandle=fsockopen("148.124.45.104",8080,$errno,$errstr);
if(!$urlHandle){echo "Unable to perform an fsockopen. Error occured: ($errno) [$errstr] - Debuginfo: (urlHandle): [$urlHandle]";}
else{
  $urlString="POST http://www.somesite.com/site.php HTTP/1.0\r\nHost: 148.124.45.104\r\nUser-Agent: Mozilla\r\nProxy-Authorization: Basic cGhpbGlwcC5rcnVlZ2VyOjEyMzQ1\r\n";
  $urlString.="Content-Type: application/x-www-form-urlencoded\r\nContent-length: ".strlen($poststring)."\r\nConnection: close\r\n\r\n$poststring\r\n\r\n";
  fputs($urlHandle,$urlString);
  $buffer="";
  for($i=0;$i<100;$i++){
    $buffer.=fgets($urlHandle,1024);
  }
  echo $buffer;
  fclose($urlHandle);