php中fsockopen用法實(shí)例
本文實(shí)例講述了php中fsockopen用法。分享給大家供大家參考。
函數(shù)說(shuō)明:fsockopen — 打開(kāi)一個(gè)網(wǎng)絡(luò)連接或者一個(gè)Unix套接字連接
語(yǔ)法:
resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )
參數(shù):
- hostname 如果安裝了OpenSSL,那么你也許應(yīng)該在你的主機(jī)名地址前面添加訪(fǎng)問(wèn)協(xié)議ssl://或者是tls://,從而可以使用基于TCP/IP協(xié)議的SSL或者TLS的客戶(hù)端連接到遠(yuǎn)程主機(jī)。
- port 端口號(hào)。如果對(duì)該參數(shù)傳一個(gè)-1,則表示不使用端口,例如unix://。
- errno 如果errno的返回值為0,而且這個(gè)函數(shù)的返回值為 FALSE ,那么這表明該錯(cuò)誤發(fā)生在套接字連接(connect())調(diào)用之前,導(dǎo)致連接失敗的原因最大的可能是初始化套接字的時(shí)候發(fā)生了錯(cuò)誤。
- errstr 錯(cuò)誤信息將以字符串的信息返回。
- timeout 設(shè)置連接的時(shí)限,單位為秒。
返回值:
fsockopen() 將返回一個(gè)文件句柄,之后可以被其他文件類(lèi)函數(shù)調(diào)用(例如: fgets() , fgetss() , fwrite() , fclose() 還有 feof() )。如果調(diào)用失敗,將返回 FALSE 。
php fsockopen使用案例
1、fsockopen 來(lái)模擬生成 HTTP 連接
$url="http://www.dbjr.com.cn";
$port=80;
$t=30;
/**fsockopen 抓取頁(yè)面
* @parem $url 網(wǎng)頁(yè)地址
* @parem $port 端口 默認(rèn) 80
* @parem $t 設(shè)置連接的時(shí)間 默認(rèn)30s
* */
function fsock($url,$port=80,$t=30)
{ $info=parse_url($url);
$fp = fsockopen($info['host'],$port,$errno,$errstr,$t);
if (!$fp)
{
echo "$errstr ($errno)<br />\n";
}
else
{
$out = "GET ".$info['path']." HTTP/1.1".PHP_EOL; $out .= "Host: ".$info['host'].".PHP_EOL; $out .= "Connection: Close".PHP_EOL.PHP_EOL;
fwrite($fp, $out);
$content = '';
while (!feof($fp))
{
$content .= fgets($fp);
}
echo $content;
fclose($fp);
}
}// 函數(shù)調(diào)用
fsock($url, $port,$t);2、PHP fsockopen模擬POST/GET方法
fsockopen除了像上面實(shí)例模擬生成 HTTP 連接之外,還能實(shí)現(xiàn)很多功能,比如模擬post 和 get 傳送數(shù)據(jù)的方法。
$url = "http://localhost/test/test.php"; #url 地址必須 http://xxxxx
$port=80;
$t=30;
$data = array(
'foo'=>'bar',
'baz'=>'boom',
'site'=>'www.dbjr.com.cn',
'name'=>'nowa magic');
/**fsockopen 抓取頁(yè)面
* @parem $url 網(wǎng)頁(yè)地址 host 主機(jī)地址 * @parem $port 網(wǎng)址端口 默認(rèn)80 * @parem $t 腳本請(qǐng)求時(shí)間 默認(rèn)30s
* @parem $method 請(qǐng)求方式 get/post
* @parem $data 如果單獨(dú)傳數(shù)據(jù)為 post 方式
* @return 返回請(qǐng)求回的數(shù)據(jù)
* */
function sock_data($url,$port=80,$t=30,$method='get',$data=null)
{
$info=parse_url($url);
$fp = fsockopen($info["host"],$port, $errno, $errstr,$t);
// 判斷是否有數(shù)據(jù)
if(isset($data) && !empty($data))
{
$query = http_build_query($data); // 數(shù)組轉(zhuǎn)url 字符串形式
}else
{
$query=null;
}
// 如果用戶(hù)的$url "http://www.dbjr.com.cn/"; 缺少 最后的反斜杠
if(!isset($info['path']) || empty($info['path']))
{
$info['path']="/index.html";
}
// 判斷 請(qǐng)求方式
if($method=='post')
{
$head = "POST ".$info['path']." HTTP/1.0".PHP_EOL;
}else
{
$head = "GET ".$info['path']."?".$query." HTTP/1.0".PHP_EOL;
}
$head .= "Host: ".$info['host'].PHP_EOL; // 請(qǐng)求主機(jī)地址
$head .= "Referer: http://".$info['host'].$info['path'].PHP_EOL;
if(isset($data) && !empty($data) && ($method=='post'))
{
$head .= "Content-type: application/x-www-form-urlencoded".PHP_EOL;
$head .= "Content-Length: ".strlen(trim($query)).PHP_EOL;
$head .= PHP_EOL;
$head .= trim($query);
}else
{
$head .= PHP_EOL;
}
$write = fputs($fp, $head); //寫(xiě)入文件(可安全用于二進(jìn)制文件)。 fputs() 函數(shù)是 fwrite() 函數(shù)的別名
while (!feof($fp))
{
$line = fread($fp,4096);
echo $line;
}
}
// 函數(shù)調(diào)用
sock_data($url,$port,$t,'post',$data);3、fsockopen以Socket方式模擬HTTP下載文件
PHP設(shè)置腳本最大執(zhí)行時(shí)間的三種方法
1.php.ini文件中 max_execution_time = 120;
2、通過(guò)PHP的ini_set函數(shù)設(shè)置
ini_set("max_execution_time", "120");
3、通過(guò)set_time_limit 函數(shù)設(shè)置
set_time_limit(120);
以上幾個(gè)數(shù)字設(shè)置為0則無(wú)限制,腳本會(huì)一直執(zhí)行下去,直到執(zhí)行結(jié)束。
set_time_limit(0);
$url = 'http://localhost/test/img.zip';
$port = '80';
/** sockopen 下載文件
* @parem $url 訪(fǎng)問(wèn)文件的url 地址
* @parem $port 默認(rèn) 80 * @parem $down_name 下載指定路徑文件名稱(chēng) 例如 ../aa.zip
* */
function sock_down($url,$port=80,$down_name=null)
{
$info=parse_url($url);
# 建立連接
$fp = fsockopen($info["host"],$port,$errno,$errstr,$t);
/*
為資源流設(shè)置阻塞或者阻塞模式 參數(shù):資源流(),0是非阻塞,1是阻塞
bool stream_set_blocking ( resource $stream , int $mode )
阻塞的好處是,排除其它非正常因素,阻塞的是按順序執(zhí)行的同步的讀取。將會(huì)一直等到從資源流里面獲取到數(shù)據(jù)才能返回
而非阻塞,因?yàn)椴槐氐却齼?nèi)容,所以能異步的執(zhí)行,現(xiàn)在讀到讀不到都沒(méi)關(guān)系,執(zhí)行讀取操作后會(huì)立即返回?cái)?shù)據(jù)
* */
stream_set_blocking($fp, 1);
if(!$fp)
{
echo "$errno : $errstr<br/>";
}
else
{
# 發(fā)送一個(gè)HTTP請(qǐng)求信息頭
$request_header="GET ".$info['path']." HTTP/1.1".PHP_EOL;
# 起始行
# 頭域
$request_header.="Host: ".$info["host"].PHP_EOL;
# 再一個(gè)回車(chē)換行表示頭信息結(jié)束
$request_header.=PHP_EOL;
# 發(fā)送請(qǐng)求到服務(wù)器
fputs($fp,$request_header);
if(!isset($down_name) || empty($down_name))
{
$down_name=basename($url); //默認(rèn)當(dāng)前文件同目錄
}
# 接受響應(yīng)
$fp2=fopen($down_name,'w'); // 要下載的文件名 下載到指定目錄
$line='';
while (!feof($fp))
{
$line.= fputs($fp2,fgets($fp));
}
if(feof($fp))
{
echo "<script>alert('已下載到當(dāng)前目錄')</script>";
}
# 關(guān)閉
fclose($fp2);
fclose($fp);
}
}
//函數(shù)調(diào)用
sock_down($url,$port);4、使用 fsockopen 偽造來(lái)源網(wǎng)址、路徑
http://www.dbjr.com.cn/article/93173.htm
下面是其他網(wǎng)友的補(bǔ)充
具體實(shí)現(xiàn)方法如下:
實(shí)例一
$fp=fsockopen("127.0.0.1",80);???? //打開(kāi)數(shù)據(jù)流
if(!$fp)?????????? //如果打開(kāi)出錯(cuò)
{
? echo "unable to openn";?????? //輸出內(nèi)容
}
else??????????? //如果成功打開(kāi)
{
? fwrite($fp,"get / http/1.0rnrn");???? //向數(shù)據(jù)流寫(xiě)入內(nèi)容
? stream_set_timeout($fp,2);?????? //進(jìn)行超時(shí)設(shè)置
? $res=fread($fp,2000);??????? //讀取內(nèi)容
? $info=stream_get_meta_data($fp);????? //獲取數(shù)據(jù)流報(bào)頭
? fclose($fp);????????? //關(guān)閉數(shù)據(jù)流
? if($info['timed_out'])??????? //如果超時(shí)
? {
??? echo 'connection timed out!';????? //輸出內(nèi)容
? }
? else
? {
??? echo $res;????????? //輸出讀取內(nèi)容
? }
}實(shí)例二
//創(chuàng)建服務(wù)端
$socket=stream_socket_server("tcp://0.0.0.0:8000",$errno,$errstr);
//如果創(chuàng)建失敗
if(!$socket)
{
? echo "$errstr ($errno)<br />n";
}
//如果創(chuàng)建成功
else
{
? //接受連接
? while($conn=stream_socket_accept($socket))
? {
??? //寫(xiě)入數(shù)據(jù)
??? fwrite($conn,'the local time is '.date('n/j/y g:i a')."n");
??? //關(guān)閉連接
??? fclose($conn);
? }
? //關(guān)閉socket
? fclose($socket);
}
//
$file="test.txt";?????? //定義文件
$fp=fopen($file,"w");????? //打開(kāi)數(shù)據(jù)流
if($fp)??????? //如果成功打開(kāi)
{
? stream_set_write_buffer($fp,0);?? //設(shè)置緩沖區(qū)
? fwrite($fp,$output);????? //寫(xiě)入內(nèi)容
? fclose($fp);?????? //關(guān)閉數(shù)據(jù)流
}希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- php url路由入門(mén)實(shí)例
- PHP URL路由類(lèi)實(shí)例
- php處理restful請(qǐng)求的路由類(lèi)分享
- php編寫(xiě)一個(gè)簡(jiǎn)單的路由類(lèi)
- PHP實(shí)現(xiàn)一個(gè)簡(jiǎn)單url路由功能實(shí)例
- php使用fsockopen函數(shù)發(fā)送post,get請(qǐng)求獲取網(wǎng)頁(yè)內(nèi)容的方法
- 淺談php中curl、fsockopen的應(yīng)用
- 基于php socket(fsockopen)的應(yīng)用實(shí)例分析
- php定時(shí)計(jì)劃任務(wù)與fsockopen持續(xù)進(jìn)程實(shí)例
- PHP操作路由器實(shí)現(xiàn)方法示例
相關(guān)文章
談PHP生成靜態(tài)頁(yè)面分析 模板+緩存+寫(xiě)文件
談PHP生成靜態(tài)頁(yè)面 模板+緩存+寫(xiě)文件,大家可以參考下代碼。2009-08-08
PHP GD庫(kù)生成圖像的幾個(gè)函數(shù)總結(jié)
這篇文章主要介紹了PHP GD庫(kù)生成圖像的幾個(gè)函數(shù)總結(jié),即imagegif、imagejpeg、imagepng、imagewbmp幾個(gè)生成圖片的函數(shù),需要的朋友可以參考下2014-11-11
php正則取img標(biāo)記中任意屬性(正則替換去掉或改變圖片img標(biāo)記中的任意屬性)
因有一項(xiàng)目新聞發(fā)布系統(tǒng),數(shù)據(jù)庫(kù)內(nèi)容字段中存儲(chǔ)的是原圖的路徑(當(dāng)然還有其他文字內(nèi)容啦,內(nèi)容里插圖時(shí),存的是圖片路徑),但前臺(tái)想使用縮略圖,琢磨1小時(shí)余,得到以下結(jié)果,可解決問(wèn)題2013-08-08
基于HBase Thrift接口的一些使用問(wèn)題及相關(guān)注意事項(xiàng)的詳解
本篇文章是對(duì)HBase Thrift接口的一些使用問(wèn)題及相關(guān)注意事項(xiàng)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP門(mén)面模式實(shí)現(xiàn)簡(jiǎn)單的郵件發(fā)送示例
這篇文章主要為大家介紹了PHP門(mén)面模式實(shí)現(xiàn)簡(jiǎn)單的郵件發(fā)送示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

