Table of Contents
guzzlehttp/guzzle
php_curl
guzzlehttp
jenssegers_date
php_date
chumper/zipper
php_zip
anchu/ ftp
php_ftp
Home Backend Development PHP Tutorial PHP extension package: a brief introduction to the extension package that can replace PHP native functions

PHP extension package: a brief introduction to the extension package that can replace PHP native functions

Aug 14, 2018 pm 03:54 PM

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 "??"

guzzlehttp/guzzle

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

<?php
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, &#39;http://www.baidu.com&#39;);
    //设置头文件的信息作为数据流输出
    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);
Copy after login

guzzlehttp

use GuzzleHttp\Client;

$client = new GuzzleHttp\Client();

$response = $client->request('POST', 'http://www.baidu.com', [
    'form_params' => [
        'username' => 'coder',
        'password' => '12345'
    ]
]);

print_r($response);
Copy after login

jenssegers/date

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

php_date

date("Ym", strtotime("-1 day"));  //获取前一天的日期 

date("Ym", strtotime("+1 day"));  //获取后一天的日期
Copy after login

jenssegers_date

(new Date('-1 day'))->format ('Ym');  // 获取前一天的日期 

(new Date('+1 day'))->format ('Ym');  //获取后一天的日期
Copy after login

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.

chumper/zipper

composer require chumper/zipper
Using this package can simplify the complexity of using the zip function of PHP itself

php_zip

<?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, &#39;/&#39;) +1);
            if(!is_dir($file_name) && $file_name) {
                $save_path = $dir .&#39;/&#39;. $file_name;
                if(file_exists($save_path)) {
                echo &#39;文件夹内已存在文件 "&#39; . $file_name . &#39;" <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);
Copy after login

chumper/zipper

Zipper::make('test.zip')->folder('test')->extractTo('foo');
Copy after login

It’s so obvious that I don’t think I need to explain anything.

anchu/ftp

composer require anchu/ftp
This package can simplify the process of php's own ftp upload code

php_ftp

<?php
$host = &#39;10.0.0.42&#39;;
$user = &#39;uftp&#39;;
$pwd = &#39;uftp&#39;;
 
// 进行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, &#39;bbb.mp3&#39;, &#39;/root/liang/ftp/bbb.mp3&#39;, FTP_BINARY);
if(!$result){
    echo "upload file fail\n";
    exit(1);
}else{
    echo "upload file success\n";
    exit(0);
}
Copy after login

anchu/ ftp

Config::set(&#39;ftp.connections.key&#39;, array(
   &#39;host&#39;   => '',
   'username' => '',
   'password'   => '',
   'passive'   => false,
   'secure'   => false,
));
FTP::uploadFile($fileFrom,$fileTo,$mode)
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles