Home >Backend Development >PHP Tutorial >PHP extension package: a brief introduction to the extension package that can replace PHP native functions
The content of this article is about PHP expansion packages: a brief introduction to expansion packages that can replace PHP native functions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .
Although programmers are making wheels all the time, there are also differences in efficiency in making wheels. Only by using good wheels can you create a good "??"
composer require guzzlehttp/guzzle
You can use guzzlehttp to completely replace curl, file_get_content, fopen and other functions. This expansion pack is extremely easy to use. Let’s look at the comparison in terms of code size.
<?php //初始化 $curl = curl_init(); //设置抓取的url curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); //设置头文件的信息作为数据流输出 curl_setopt($curl, CURLOPT_HEADER, 1); //设置获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //设置post方式提交 curl_setopt($curl, CURLOPT_POST, 1); //设置post数据 $post_data = array( "username" => "coder", "password" => "12345" ); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); //执行命令 $data = curl_exec($curl); //关闭URL请求 curl_close($curl); //显示获得的数据 print_r($data);
use GuzzleHttp\Client; $client = new GuzzleHttp\Client(); $response = $client->request('POST', 'http://www.baidu.com', [ 'form_params' => [ 'username' => 'coder', 'password' => '12345' ] ]); print_r($response);
composer require jenssegers/date
Use this extension package to let the php program The implementation of date-related requirements by employees is more concise and simple. Please see the comparison below
date("Ym", strtotime("-1 day")); //获取前一天的日期 date("Ym", strtotime("+1 day")); //获取后一天的日期
(new Date('-1 day'))->format ('Ym'); // 获取前一天的日期 (new Date('+1 day'))->format ('Ym'); //获取后一天的日期
It is obvious that the new method is more intuitive for date processing. Of course, this is a simple application, and it will be more advantageous in complex date calculations.
composer require chumper/zipper
Using this package can simplify the complexity of using the zip function of PHP itself
<?php $resource = zip_open($filename); while($zip = zip_read($resource)) { if(zip_entry_open($resource, $zip)) { $file_content = zip_entry_name($zip); $file_name = substr($file_content, strrpos($file_content, '/') +1); if(!is_dir($file_name) && $file_name) { $save_path = $dir .'/'. $file_name; if(file_exists($save_path)) { echo '文件夹内已存在文件 "' . $file_name . '" <pre />'; }else { echo $file_name . '<pre />'; $file_size = zip_entry_filesize($zip); $file = zip_entry_read($zip, $file_size); file_put_contents($save_path, $file); zip_entry_close($zip); } } } } zip_close($resource);
Zipper::make('test.zip')->folder('test')->extractTo('foo');
It’s so obvious that I don’t think I need to explain anything.
composer require anchu/ftp
This package can simplify the process of php's own ftp upload code
<?php $host = '10.0.0.42'; $user = 'uftp'; $pwd = 'uftp'; // 进行ftp连接,根据port是否设置,传递的参数会不同 if(empty($port)){ $f_conn = ftp_connect($host); }else{ $f_conn = ftp_connect($host, $port); } if(!$f_conn){ echo "connect fail\n"; exit(1); } echo "connect success\n"; // 进行ftp登录,使用给定的ftp登录用户名和密码进行login $f_login = ftp_login($f_conn,$user,$pwd); if(!$f_login){ echo "login fail\n"; exit(1); } echo "login success\n"; // 获取当前所在的ftp目录 $in_dir = ftp_pwd($f_conn); if(!$in_dir){ echo "get dir info fail\n"; exit(1); } echo "$in_dir\n"; // 获取当前所在ftp目录下包含的目录与文件 $exist_dir = ftp_nlist($f_conn, ftp_pwd($f_conn)); print_r($exist_dir); // 要求是按照日期在ftp目录下创建文件夹作为文件上传存放目录 echo date("Ymd")."\n"; $dir_name = date("Ymd"); // 检查ftp目录下是否已存在当前日期的文件夹,如不存在则进行创建 if(!in_array("$in_dir/$dir_name", $exist_dir)){ if(!ftp_mkdir($f_conn, $dir_name)){ echo "mkdir fail\n"; exit(1); }else{ echo "mkdir $dir_name success\n"; } } // 切换目录 if(!ftp_chdir($f_conn, $dir_name)){ echo "chdir fail\n"; exit(1); }else{ echo "chdir $dir_name success\n"; } // 进行文件上传 $result = ftp_put($f_conn, 'bbb.mp3', '/root/liang/ftp/bbb.mp3', FTP_BINARY); if(!$result){ echo "upload file fail\n"; exit(1); }else{ echo "upload file success\n"; exit(0); }
Config::set('ftp.connections.key', array( 'host' => '', 'username' => '', 'password' => '', 'passive' => false, 'secure' => false, )); FTP::uploadFile($fileFrom,$fileTo,$mode)
Related recommendations:
redis PHP extension package installation method
##php Install xdebug extension, phpxdebug extension
php extension and embedding--c extension development helloworld
The above is the detailed content of PHP extension package: a brief introduction to the extension package that can replace PHP native functions. For more information, please follow other related articles on the PHP Chinese website!