Php最让我感觉不错的地方是这个函数fsockopen,记得以前的B2C网站就是通过这个函数实现前台和订单处理系统的交互。还有一个以curl_开头的函数,可以实现很多功能。有时间要好好研究!下面是关于fscokopen的介绍
1.fsockopen函数说明:
Open Internet or Unix domain socket connection(打开套接字链接)
Initiates a socket connection to the resource specified by target .
fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets() , fgetss() , fwrite() , fclose() , and feof() ).就是返回一个文件句柄
2.实例一:跨站点请求
function getblogdata($ip,$condition,$url){
$req=$condition;
$header .= "POST $url HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ($ip,80, $errno, $errstr,30);
if(!$fp){
_log('--ERROR--'.$ip.' can not open');
}else{
while (!feof($fp)) {
fputs ($fp, $header . $req);
$res = fgets ($fp, 204800);
}
}
fclose ($fp);
return $res;
}
使用:
$domain='blog.m6699.com';
$file='http://blog.m6699.com/deccm_interface.php';
$condition='limit=12&count='.$usernum;
$data17=getblogdata($domain,$condition,$file);
3.实例二:抓取网页
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
4。其他功能:
好像还可以实现HTTP下载,有知道和补充的网友欢迎留言!