2013年2月17日星期日

详解PHP fsockopen的使用方法


PHP fsockopen函数对于我们刚刚接触PHP语言的人来说,还是不太了解其具体功能。希望通过本文介绍的内容能对大家起到一定的帮助。PHP fsockopen是一个功能比较强大的函数。我们在这篇文章中将会对这个函数做一个具体的介绍,希望对大家有所帮助。记得以前的B2C网站就是通过这个函数实现前台和订单处理系统的交互。
1.PHP 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() ).就是返回一个文件句柄

开启PHP fsockopen这个函数

PHP fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启。


$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);  
}
以上就是PHP fsockopen函数的具体使用方法,供大家参考学习。

没有评论:

发表评论