#php函数01#

本文介绍了PHP中处理文件的基本函数,如打开、读取、写入和关闭文件的方法,并详细讲解了网络编程中URL解析、HTTP请求构建及数据流控制的相关函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

array_change_key_case   将数组的所有的键转换为大写字母:
get_resource_type
fclose               函数关闭一个打开文件。
fread 函数读取文件(可安全用于二进制文件)。
fwrite    函数将内容写入一个打开的文件中。
bindec   二进制转换为十进制
chr
str_split    函数把字符串分割到数组中。
ord         函数返回字符串的首个字符的 ASCII 值。

 


#解析区别#
parse_str函数将查询字符串解析到变量中,
parse_url函数用于解析整个URL,并返回其组成部分。

案例:
 $test = parse_url("http://localhost/index.php?name=tank&sex=1#top");
 print_r($test);

Array
(
 [scheme] => http //使用什么协议
 [host] => localhost //主机名
 [path] => /index.php //路径
 [query] => name=tank&sex=1 // 所传的参数
 [fragment] => top //后面根的锚点
)

 

 


#php函数http_build_query#
函数的作用是使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串。

案例:
$data = array("name"=>"callback" , "value"=>"test");
$rescult = http_build_query($data);
我们输出下$rescutl可以得到:
name=callback&value=test

#php函数stream_context_create#

1:函数原型:resource stream_context_create ([ array $options [, array $params ]] ) 
2:说明:创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()
   等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。 
3:用例1:

$opts = array( 'http-->array( 
'method'=>"GET", 
'header'=>"Accept-language: en\r\n" . 
"Cookie: foo=bar\r\n" 

); 
$context = stream_context_create($opts);  
$fp = fopen('//www.jb51.net', 'r', false, $context); 
fpassthru($fp); 
fclose($fp); 


4:用例2

$opts = array('http' => array('proxy' => 'tcp://127.0.0.1:8080', 'request_fulluri' => true)); 
$context = stream_context_create($opts); 
$data = file_get_contents('//www.jb51.net', false, $context); 
echo $data; 

#php函数stream_socket_client#
使用场景
海关加密中使用加签,需要另外一台服务器,服务器使用的是websocket服务
php对接websocket服务

1:stream函数实现
服务器端案例

 set_time_limit(0);
  
 class SocketServer
 {
       public function __construct($port)
      {
           global $errno, $errstr;

         $socket = stream_socket_server('tcp://127.0.0.1:'.$port, $errno, $errstr);
        while($conn = stream_socket_accept($socket, -1))
        {
             $buff = '';
             $data = '';

           //读取请求数据直到遇到\r\n结束符
            while(!preg_match('#\r\n#', $buff))
            {
               $buff = fread($conn, 1024);
               $data .= preg_replace('#\r\n#', '', $buff);
             }
            fwrite($conn, $data);
            fclose($conn);
        }
        fclose($socket);
    }
}

new SocketServer(1212);


客户端

  $socket = stream_socket_client('tcp://127.0.0.1:1212', $errno, $errstr);
      if(!$socket)
          {
              die($errno.$errstr);
         }
         else
         {
             fwrite($socket, "\r\n");
             $response = fread($socket, 1024);
             file_put_contents('log.txt', date("[H:i:s] ", time()).$response."\n", FILE_APPEND);
             fclose($socket);
        }
    
 

 

 

#php函数stream_set_timeout#
PHP函数stream_set_timeout(Stream Functions)作用于读取流时的时间控制
防止出现超时报错

1:使用案例
$server="www.yahoo.com";
$port = 80;
$data="GET / HTTP/1.0rn";
$data.="Connection: Closern";
$data.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)rnrn";

$start_time = time();
$fp=fsockopen($server, $port, $errno, $errstr, 5);
if (!$fp) {
die("Connect Timeout.n");
} else {
stream_set_blocking($fp, True);
stream_set_timeout($fp, 3);
fputs($fp, "$data");
while (!feof($fp)) {
$text .= fread($fp, 2000);
$diff = time() - $start_time;
if ($diff > 24) {
die("Timeout!n");
}
$status = stream_get_meta_data($fp);
if ($status[’timed_out’]) {
die("Stream Timeout!n");
}
}
}
fclose($fp);
 

 

 

#php函数get_resource_type#
返回资源类型
使用案例
get_resource_type ( resource $handle ) : string

$c = mysql_connect();
echo get_resource_type($c)."\n";
// 打印:mysql link

$fp = fopen("foo","w");
echo get_resource_type($fp)."\n";
// 打印:file

$doc = new_xmldoc("1.0");
echo get_resource_type($doc->doc)."\n";
// 打印:domxml document
 

 

#php函数stream_get_meta_data#
stream_get_meta_data函数主要功能是从封装协议
文件指针中取得报头/元数据

案例:
$fp=fopen("http://www.sina.com.cn", 'r');
$stream_meta = stream_get_meta_data($fp);
print_r($stream_meta);

输出
Array
(
     [wrapper_data] => Array
         (
             [0] => HTTP/1.0 200 OK
             [1] => Date: Tue, 06 Dec 2011 10:08:11 GMT
             [2] => Server: Apache
             [3] => Last-Modified: Tue, 06 Dec 2011 10:07:12 GMT
             [4] => Accept-Ranges: bytes
             [5] => X-Powered-By: mod_xlayout/rc2
             [6] => Cache-Control: max-age=60
             [7] => Expires: Tue, 06 Dec 2011 10:09:11 GMT
             [8] => Vary: Accept-Encoding
             [9] => X-UA-Compatible: IE=EmulateIE7
             [10] => Content-Type: text/html
             [11] => Age: 26
             [12] => Content-Length: 675274
             [13] => X-Cache: HIT from xd33-98.HP08040037.sina.com.cn
             [14] => Connection: close
         )

     [wrapper_type] => http
     [stream_type] => tcp_socket/ssl
     [mode] => r
     [unread_bytes] => 3759
     [seekable] =>  
     [uri] => http://www.sina.com.cn
     [timed_out] =>  
     [blocked] => 1
     [eof] =>  
)
 

 

#php函数#
从不同的 ASCII 值返回字符:
echo chr(61) . "<br>"; // 十进制
echo chr(061) . "<br>"; // 八进制值
echo chr(0x61) . "<br>"; // 十六进制值

-----------------------------------------------------------------------------------

以下两个函数作用差不多

wordwrap  按照指定长度对字符串进行折行处理:
chunk_split 在每个字符后分割一次字符串,并在每个分割后添加 ".":

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值