《PHP教程:php中fsockopen用法實例》要點:
本文介紹了PHP教程:php中fsockopen用法實例,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP實戰(zhàn)本文實例講述了php中fsockopen用法.分享給大家供大家參考.
PHP實戰(zhàn)具體實現(xiàn)辦法如下:
代碼如下:
$fp=fsockopen("127.0.0.1",80);???? //打開數(shù)據(jù)流
if(!$fp)?????????? //如果打開出錯
{
? echo "unable to openn";?????? //輸出內(nèi)容
}
else??????????? //如果成功打開
{
? fwrite($fp,"get / http/1.0rnrn");???? //向數(shù)據(jù)流寫入內(nèi)容
? stream_set_timeout($fp,2);?????? //進行超時設(shè)置
? $res=fread($fp,2000);??????? //讀取內(nèi)容
? $info=stream_get_meta_data($fp);????? //獲取數(shù)據(jù)流報頭
? fclose($fp);????????? //關(guān)閉數(shù)據(jù)流
? if($info['timed_out'])??????? //如果超時
? {
??? echo 'connection timed out!';????? //輸出內(nèi)容
? }
? else
? {
??? echo $res;????????? //輸出讀取內(nèi)容
? }
}
?
//實例二
?
//創(chuàng)建服務端
$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))
? {
??? //寫入數(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");????? //打開數(shù)據(jù)流
if($fp)??????? //如果成功打開
{
? stream_set_write_buffer($fp,0);?? //設(shè)置緩沖區(qū)
? fwrite($fp,$output);????? //寫入內(nèi)容
? fclose($fp);?????? //關(guān)閉數(shù)據(jù)流
}
PHP實戰(zhàn)希望本文所述對大家的php程序設(shè)計有所贊助.
歡迎參與《PHP教程:php中fsockopen用法實例》討論,分享您的想法,維易PHP學院為您提供專業(yè)教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/12830.html