本文实例讲述了PHP使用fsockopen函数发送post,get请求获取网页内容的方法。分享给大家供大家参考。
具体实现代码如下:
if($post) {
$out = "post $path http/1.0 ";
$out .= "accept: / ";
//$out .= "referer: $boardurl ";
$out .= "accept-language: zh-cn ";
$out .= "content-type: application/x-www-form-urlencoded ";
$out .= "user-agent: $_server[http_user_agent] ";
$out .= "host: $host ";
$out .= 'content-length: '.strlen($post)." ";
$out .= "connection: close ";
$out .= "cache-control: no-cache ";
$out .= "cookie: $cookie ";
$out .= $post;
} else {
$out = "get $path http/1.0 ";
$out .= "accept: / ";
//$out .= "referer: $boardurl ";
$out .= "accept-language: zh-cn ";
$out .= "user-agent: $_server[http_user_agent] ";
$out .= "host: $host ";
$out .= "connection: close ";
$out .= "cookie: $cookie ";
}
$fp = @fsockopen(($ip ? $ip : $host),$port,$errno,$errstr,$timeout);
if(!$fp)
{
return '';//note $errstr : $errno
} else {
return '成功访问';
}
fsockopen语法:<div class="codetitle"><a style="CURSOR: pointer" data="94358" class="copybut" id="copybut94358" onclick="doCopy('code94358')"> 代码如下: