Hi All,
I've beenusing fsockopen for a while to perform POST operations in PHP
Currently, I'm trying to post to a windows server that uses ASP, and it doesn't want to know.
I send it:
POST /uk/lookup.asmx/NearestRadial HTTP/1.1
Host: services.postcodeanywhere.co.uk
Content-Type: application/x-www-form-urlencoded
Connection: close
Cache-Control: no-cache
Content-Length: 218
Origin=m44+6aj&Destination=M30+0AR&Destination=CW9+5HD&Destination=WN3+4NJ&Destination=M17+8PG&Destination=M33+3QN&Radius=5&Items=0&Units=MILES&AccountCode=HENDE11111&LicenseCode=RG17-GJ99-RB49-GD31&MachineId=webserver
And it replies:
HTTP/1.1 100 Continue
HTTP/1.1 500 Internal Server Error.
Connection: close
Date: Sun, 02 May 2004 11:20:40 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 491
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
funny, how it first starts to send a "100 continue" message before it changes its mind and sends the eternal server error.
I'm assuming that there's something in the way I format the header thatASP doesn't like.
here's my code:
function botherPCAnywhere($server, $port, $url, $origin,$destinations,$vars){
//define variables
$urlencoded = "";
$return=true;
//encode the origin
$urlencoded.= urlencode("Origin") . "=" . urlencode($origin). "&";
//encode the destinations
foreach ($destinations as $destination){
$urlencoded.= urlencode("Destination") . "=" . urlencode($destination). "&";
}
//encode the other values
//
while (list($key,$value) = each($vars)) $urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
$urlencoded = substr($urlencoded,0,-1);
$urlencoded.="\r\n\r\n";
$content_length = strlen($urlencoded);
//format the info into HTTP headers
//
$headers.= "POST $url HTTP/1.1\r\n";
$headers.= "Host: $server\r\n";
$headers.= "Content-Type: application/x-www-form-urlencoded\r\n";
$headers.= "Connection: close\r\n";
$headers.= "Content-Length: $content_length\r\n\r\n";
echo $headers;
echo $urlencoded;
print "the server said<br>";
//open the socket
$fp = fsockopen($server, $port, $errno, $errstr);
//if that didn't work, return false
if (!$fp) {
print "the Socket said: $errstr ($errno)<br>\n";
$return =false;
}
//send the headers
fputs($fp, $headers);
//send the POST data
fputs($fp, $urlencoded);
//untill we hit the end of file (eof) set the return var to the last line sent
//$ret = "";
while (!feof($fp)) {
echo fgets ($fp,4096);
}
//close the socket
fclose($fp);
return $return;
}//# end http_post function ########################################
Thanks for any help
Matt