Here's a script that works for me:
function post($data_arr, $url, &$err_no, &$err_str)
{
if (substr($url, 0, 7) != 'http://') {
$url = 'http://' . $url;
}
$parse_url = parse_url($url);
echo '<b>$parse_url:</b><br />';
foreach ($parse_url as $key => $value) {
echo ' ' . $key . ' => ' . $value . '<br />';
}
echo '<br />';
$post_str = '';
foreach ($data_arr as $key => $value) {
if ($post_str) {
$post_str .= '&';
}
$post_str .= $key . '=' . urlencode($value);
}
echo '<b>$post_str:</b> ' . $post_str . '<br />';
echo '<br />';
$url = str_replace('http://', '', $url);
echo '<b>$url:</b> ' . $url . '<br />';
echo '<br />';
$host = substr($url, 0, strpos($url, '/'));
echo '<b>$host:</b> ' . $host . '<br />';
echo '<br />';
$uri = strstr($url, '/');
echo '<b>$uri:</b> ' . $uri . '<br />';
echo '<br />';
$hdr['POST'] = $parse_url['path'] . ' HTTP/1.0';
$hdr['Accept'] = 'application/x-www-form-urlencoded';
$hdr['Referer'] = 'http://' . $_SERVER['SERVER_NAME'] .
$_SERVER['PHP_SELF'];
$hdr['Accept-Language'] = 'en-us';
$hdr['Content-Type'] = 'application/x-www-form-urlencoded';
$hdr['Accept-Encoding'] = 'gzip, deflate';
$hdr['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 6.0; ' .
'Windows NT 5.1; SV1; .NET CLR 1.1.4322)';
$hdr['Host'] = $parse_url['host'];
$hdr['Content-Length'] = strlen($post_str);
$hdr['Connection'] = 'Keep-Alive';
$hdr['Cache-Control'] = 'no-cache';
$header = '';
foreach ($hdr as $key => $value) {
if ($key == 'POST') {
$header .= $key . ' ' . $value . "\r\n";
} else {
$header .= $key . ': ' . $value . "\r\n";
}
}
$request = $header . "\r\n" . $post_str;
echo '<b>$request:</b><br />';
echo nl2br($request) . '<br />';
echo '<br />';
$socket = fsockopen($host, 80, $err_no, $err_str, 20);
if (!$socket) {
$err_no = $result['errno'];
$err_str = $result['errstr'];
echo '<font color="red"><b>fsockopen() failed.</b></font><br />';
echo '<br />';
return false;
}
echo '<font color="green"><b>fsockopen() succeeded.</b></font><br />';
echo '<br />';
$idx = 0;
$fwrite = fwrite($socket, $request);
if ($fwrite == false) {
echo '<font color="red"><b>fwrite() failed.</b></font><br />';
} else {
echo '<font color="green"><b>fwrite() succeeded.</b></font><br />';
}
echo '<br />';
while (!feof($socket)) {
$response[] = fgets($socket, 128);
}
return $response;
}
All the "echo" statements are for testing/debugging only, of course. Also, some elements of the "$hdr" array can be changed or removed.
Here's some code for testing:
$data = array('test' => 'data', 'submit' => 'submit');
$url = 'http://www.example.com/';
$post_result = post($data, $url, $err_no, $err_str);
if ($post_result === false) {
echo '<font color="red"><b>Error # ' .
$err_no . ': ' . $err_str . '</b></font>';
} else {
echo '<b>$response: </b>' . '<br />';
foreach ($post_result as $value) {
echo nl2br($value);
}
}