所以我目前正在尝试实现广泛用于“GET”和“POST”程序的“SendToHost”功能。就我而言,我想用它来向购物网站的邮政编码输入表单发送“邮政编码”,以用于检索该邮政编码的特定目录。更具体地说,代码应自动生成包含邮政编码结果的网页。下面是我的代码加上函数,我想知道它为什么不起作用:
function SendToHost($host, $method, $path, $data, $useragent=0)
{
// Supply a default method of GET if the one passed was empty
if (empty($method))
$method = 'GET';
$method = strtoupper($method);
$fp = fsockopen($host,80);
if ($method == 'GET')
$path .= '?' . $data;
fputs($fp, "$method $path HTTP/1.1\n");
fputs($fp, "Host: $host\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: " . strlen($data) . "\n");
if ($useragent)
fputs($fp, "User-Agent: MSIE\n");
fputs($fp, "Connection: close\n\n");
if ($method == 'POST')
fputs($fp, $data);
while (!feof($fp))
$buf .= fgets($fp,128);
fclose($fp);
return $buf;
}
echo sendToHost('catalog.coles.com.au','get','/default.aspx','ctl00_Body_PostcodeTextBox=4122');