欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php中fsockopen用法實(shí)例

 更新時(shí)間:2023年05月07日 12:11:26   投稿:shichen2014  
這篇文章主要介紹了php中fsockopen用法,實(shí)例分析了fsockopen的創(chuàng)建、寫入及關(guān)閉等具體流程,需要的朋友可以參考下

本文實(shí)例講述了php中fsockopen用法。分享給大家供大家參考。

函數(shù)說(shuō)明:fsockopen — 打開一個(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ī)名地址前面添加訪問協(xié)議ssl://或者是tls://,從而可以使用基于TCP/IP協(xié)議的SSL或者TLS的客戶端連接到遠(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è)文件句柄,之后可以被其他文件類函數(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ù)的方法?!?/p>

$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;
    }
    // 如果用戶的$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); //寫入文件(可安全用于二進(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、通過PHP的ini_set函數(shù)設(shè)置
ini_set("max_execution_time", "120");
3、通過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 訪問文件的url 地址
 * @parem $port 默認(rèn) 80 * @parem $down_name 下載指定路徑文件名稱 例如 ../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)在讀到讀不到都沒關(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è)回車換行表示頭信息結(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);???? //打開數(shù)據(jù)流
if(!$fp)?????????? //如果打開出錯(cuò)
{
? echo "unable to openn";?????? //輸出內(nèi)容
}
else??????????? //如果成功打開
{
? fwrite($fp,"get / http/1.0rnrn");???? //向數(shù)據(jù)流寫入內(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))
? {
??? //寫入數(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ù)流
}

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論