hi
I am trying to simulate POST thru php but the script will not work because I need to specify the login credentials (username and password) of the site I am contacting. The problem is that I am not sure where I should specify these.
below is the code that I am using :
.............................................................................
<?php
////////////////////////////////////////////////////
function post_it($datastream, $url)
{
//.... process the destination URL first .... //
// remove http:// from the url
$url = preg_replace("@^http://@i", "", $url);
// extract everything that is before
// the first / once http:// has been removed
$host = substr($url, 0, strpos($url, "/"));
// the location of the page on the server.
$uri = strstr($url, "/");
///////////////////////////////////////////////
// variable which will contain all the POST
// variables and their values
$reqbody = '';
foreach($datastream as $key=>$val)
{
if (!empty($reqbody)) $reqbody.= '&';
$reqbody.= $key.'='.urlencode($val);
}
$contentlength = strlen($reqbody);
$reqheader = "POST $uri HTTP/1.1\r\n".
"Host: $host\n". "User-Agent: PostIt\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: $contentlength\r\n\r\n".
"$reqbody\r\n";
//"Connection: Close";
// open a socket connection to the server at port 80
$socket = fsockopen($host, 8000, $errno, $errstr);
if (!$socket)
{
$result["errno"] = $errno;
$result["errstr"] = $errstr;
return $result;
}
// post the infornation on the server
fputs($socket, $reqheader);
// get the ouput generated, after the script
// has processed the POST variables on the server,
// in an array
while (!feof($socket))
{
$result[] = fgets($socket, 4096);
}
fclose($socket);
return $result;
}
////////////////////////////////////////////////
// the data that you want the script on the remote server to process
$data['Page'] = 'FinalizeUser' ;
$data['ReturnPage'] = 'UserQry';
$data['Title'] = 'User%20Search';
$data['UserId'] = 'aspclient';
$data['AccountIndex'] = '13';
$data['Confirm'] = 'Confirm';
// send the data to the script
$result = post_it($data, "http://localhost/Admin");
if (isset($result["errno"]))
{
$errno = $result["errno"];
$errstr = $result["errstr"];
echo "<B>Error $errno</B> $errstr";
exit;
}
else
{
// display from row 11 onwards because
// rows 1-10 contain the header information.
// Rows 11 onwards will contain the ouput of
// the script on the remote server
for($i=0;$i< count($result); $i++)
{
echo $result[$i];
}
}
//}
?>
..............................................................................
The reply that I get is:
HTTP/1.0 401 Unauthorized Server: Extent/1.0 WWW-Authenticate: Basic realm="TestWebServer" Content-type: text/html Access denied
Now, I do know the username and password to use, but where do I specify these?
I have tried: http://admin:password@localhost/Admin"
but it does not work, I get the following error:
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known.
I am using PHP Version 5.0.2 on Windows 2000 Server and IIS 5.0.
Hearty thanks to anyone who can help.