今天在網路上找尋PHP不透過curl來達到使用POST讀取網頁或CGI的方式:
找到以下資訊確實可用:
(原文出處:http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/)
函式宣告:
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
使用方式:
echo do_post_request("http://192.168.172.203/cgi-bin/ed3.cgi","page_charset=big5&userid=admin&userpwd=123456&pagename=26&mainmenu=69&page=0");