Home >Backend Development >PHP Tutorial >24 PHP Libraries You Should Know
It’s an exciting time to be a PHP developer. There are tons of useful libraries distributed every day that are easy to discover and use on Github. Below are 24 of the coolest libraries I've ever come across. Is your favorite library not on this list? Then share it in the comments!
1. Dispatch – micro-framework
Dispatch is a small PHP framework. It doesn't give you a complete MVC setup, but you can define URL rules and methods to better organize your application. This is perfect for APIs, simple sites or prototypes.
//包含库 include 'dispatch.php'; // 定义你的路由 get('/greet', function () { //渲染视图 render('greet-form'); }); //post处理 post('/greet', function () { $name = from($_POST, 'name'); // render a view while passing some locals render('greet-show', array('name' => $name)); }); // serve your site dispatch();
You can match specific types of HTTP requests and paths, render views or do more. If you combine Dispatch with other frameworks, you can have a very powerful and lightweight program!
2. Klein – Lightning-fast routing for PHP
Klein is another lightweight routing library for PHP5.3+. Although it has a somewhat more verbose syntax than Dispatch, it is quite fast. Here's an example:
respond('/[:name]', function ($request) { echo 'Hello ' . $request->name; });
You can also customize to specify the HTTP method and use regular expressions for the path.
respond('GET', '/posts', $callback); respond('POST', '/posts/create', $callback); respond('PUT', '/posts/[i:id]', $callback); respond('DELETE', '/posts/[i:id]', $callback); //匹配多种请求方法: respond(array('POST','GET'), $route, $callback); //你或许也想在相同的地方处理请求 respond('/posts/[create|edit:action] /[i:id] ', function ($request, $response) { switch ($request->action) { // do something } });
This is great for small projects, but when you use a library like this for a large application, you have to follow the rules because your code can become unmaintainable very quickly. So you'd better go with a fully mature framework like Laravel or CodeIgniter.
3. Ham – Routing library with cache
Ham is also a lightweight routing framework, but it uses caching to achieve even faster speeds. It does this by caching anything I/O related into XCache/APC. Here's an example:
require '../ham/ham.php'; $app = new Ham('example'); $app->config_from_file('settings.php'); $app->route('/pork', function($app) { return "Delicious pork."; }); $hello = function($app, $name='world') { return $app->render('hello.html', array( 'name' => $name )); }; $app->route('/hello/<string>', $hello); $app->route('/', $hello); $app->run();
This library requires you to have at least one of XCache and APC installed, which may mean that it may not work on hosts provided by most hosting providers. But if you have a host with one of them installed, or you have control over your web server, you should try this fastest framework.
4. Assetic - Resource Management
Assetic is a PHP resource management framework for merging and reducing CSS/JS resources. Below are examples.
use Assetic/Asset/AssetCollection; use Assetic/Asset/FileAsset; use Assetic/Asset/GlobAsset; $js = new AssetCollection(array( new GlobAsset('/path/to/js/*'), new FileAsset('/path/to/another.js'), )); //当资源被输出时,代码会被合并 echo $js->dump();
Combining resources in this way is a good idea as it speeds up the site. Not only the total download volume is reduced, but also a large number of unnecessary HTTP requests are eliminated (these are the two things that most affect page load time)
5. ImageWorkshop – Image processing with layers
ImageWorkshop is a tool that allows you to control An open source library for layered images. With it you can resize, crop, create thumbnails, watermark or do more. Here's an example:
// 从norway.jpg图片初始化norway层 $norwayLayer = ImageWorkshop::initFromPath('/path/to/images/norway.jpg'); // 从watermark.png图片初始化watermark层(水印层) $watermarkLayer = ImageWorkshop::initFromPath('/path/to/images/watermark.png'); $image = $norwayLayer->getResult(); // 这是生成的图片! header('Content-type: image/jpeg'); imagejpeg($image, null, 95); // We choose to show a JPG with a quality of 95% exit;
ImageWorkshop was developed to simplify some of the most common use cases for working with images in PHP, if you need something more powerful you should check out the Imagine library!
6. Snappy - Snapshot/PDF library
Snappy is a PHP5 library that can generate snapshots, URLs, HTML, and PDFs. It relies on the wkhtmltopdf binary (available on Linux, Windows and OSX). You can use them like this:
require_once '/path/to/snappy/src/autoload.php'; use Knp/Snappy/Pdf; //通过wkhtmltopdf binary路径初始化库 $snappy = new Pdf('/usr/local/bin/wkhtmltopdf'); //通过把Content-type头设置为pdf来在浏览器中展示pdf header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="file.pdf"'); echo $snappy->getOutput('http://www.github.com');
Be aware that your hosting provider may not allow calling external binaries.
7. Idiorm - lightweight ORM library
Idiorm is my favorite one that I have used in the tutorials on this website before. It is a lightweight ORM library, a PHP5 query builder built on PDO. With it, you can forget how to write boring SQL:
$user = ORM::for_table('user') ->where_equal('username', 'j4mie') ->find_one(); $user->first_name = 'Jamie'; $user->save(); $tweets = ORM::for_table('tweet') ->select('tweet.*') ->join('user', array( 'user.id', '=', 'tweet.user_id' )) ->where_equal('user.username', 'j4mie') ->find_many(); foreach ($tweets as $tweet) { echo $tweet->text; }
Idiorm has a sister library called Paris, which is an Active Record implementation based on Idiorm.
8. Underscore – A tool belt for PHP
Underscore is an interface to the original Underscore.js – a tool belt for Javascript applications. The PHP version does not disappoint and supports almost all native features. Here are some examples:
__::each(array(1, 2, 3), function($num) { echo $num . ','; }); // 1,2,3, $multiplier = 2; __::each(array(1, 2, 3), function($num, $index) use ($multiplier) { echo $index . '=' . ($num * $multiplier) . ','; }); // prints: 0=2,1=4,2=6, __::reduce(array(1, 2, 3), function($memo, $num) { return $memo + $num; }, 0); // 6 __::find(array(1, 2, 3, 4), function($num) { return $num % 2 === 0; }); // 2 __::filter(array(1, 2, 3, 4), function($num) { return $num % 2 === 0; }); // array(2, 4)
This library also supports chain syntax, which makes it even more powerful.
9. Requests - Simple HTTP Requests
Requests is a library that simplifies HTTP requests. If you're like me and can almost never remember the various parameters passed to Curl, then this is for you:
$headers = array('Accept' => 'application/json'); $options = array('auth' => array('user', 'pass')); $request = Requests::get('https://api.github.com/gists', $headers, $options); var_dump($request->status_code); // int(200) var_dump($request->headers['content-type']); // string(31) "application/json; charset=utf-8" var_dump($request->body); // string(26891) "[…]"
With this library you can send HEAD, GET, POST, PUT, DELTE With PATCH HTTP requests, you can add files and parameters via arrays and have access to all corresponding data.
10. Buzz – Simple HTTP request library
Buzz is another library that completes HTTP requests. Here's an example:
$request = new Buzz/Message/Request('HEAD', '/', 'http://google.com'); $response = new Buzz/Message/Response(); $client = new Buzz/Client/FileGetContents(); $client->send($request, $response); echo $request; echo $response;
Because it lacks documentation, you have to read the source code to learn all the parameters it supports.
11. Goutte – Web Scraping Library
Goutte is a library for crawling websites and extracting data. It provides an elegant API that makes it easy to select specific elements from remote pages.
require_once '/path/to/goutte.phar'; use Goutte/Client; $client = new Client(); $crawler = $client->request('GET', 'http://www.symfony-project.org/'); //点击链接 $link = $crawler->selectLink('Plugins')->link(); $crawler = $client->click($link); //使用一个类CSS语法提取数据 $t = $crawler->filter('#data')->text(); echo "Here is the text: $t";
12. Carbon – DateTime library
Carbon is a simple extension of the DateTime API.
printf("Right now is %s", Carbon::now()->toDateTimeString()); printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); $tomorrow = Carbon::now()->addDay(); $lastWeek = Carbon::now()->subWeek(); $nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4); $officialDate = Carbon::now()->toRFC2822String(); $howOldAmI = Carbon::createFromDate(1975, 5, 21)->age; $noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London'); $endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT'); //总是以UTC对比 if (Carbon::now()->gte($endOfWorld)) { die(); } if (Carbon::now()->isWeekend()) { echo 'Party!'; } echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2分钟之前'
13. Ubench – Micro Benchmark Library
Ubench is a micro library for benchmarking PHP code, monitoring (code) execution time and memory usage. Here's an example:
use Ubench/Ubench; $bench = new Ubench; $bench->start(); //执行一些代码 $bench->end(); //获取执行消耗时间和内存 echo $bench->getTime(); // 156ms or 1.123s echo $bench->getTime(true); // elapsed microtime in float echo $bench->getTime(false, '%d%s'); // 156ms or 1s echo $bench->getMemoryPeak(); // 152B or 90.00Kb or 15.23Mb echo $bench->getMemoryPeak(true); // memory peak in bytes 内存峰值 echo $bench->getMemoryPeak(false, '%.3f%s'); // 152B or 90.152Kb or 15.234Mb //在结束标识处返回内存使用情况 echo $bench->getMemoryUsage(); // 152B or 90.00Kb or 15.23Mb
It's a good idea to run these validations (only) during development.
14. Validation – Input validation engine
Validation 声称是PHP库里最强大的验证引擎。但是,它能名副其实吗?看下面:
use Respect/Validation/Validator as v; //简单验证 $number = 123; v::numeric()->validate($number); //true //链式验证 $usernameValidator = v::alnum()->noWhitespace()->length(1,15); $usernameValidator->validate('alganet'); //true //验证对象属性 $user = new stdClass; $user->name = 'Alexandre'; $user->birthdate = '1987-07-01'; //在一个简单链中验证他的属性 $userValidator = v::attribute('name', v::string()->length(1,32)) ->attribute('birthdate', v::date()->minimumAge(18)); $userValidator->validate($user); //true
你可以通过这个库验证你的表单或其他用户提交的数据。除此之外,它内置了很多校验,抛出异常和定制错误信息。
15. Filterus – 过滤库
Filterus是另一个过滤库,但它不仅仅可以验证,也可以过滤匹配预设模式的输出。下面是一个例子:
$f = Filter::factory('string,max:5'); $str = 'This is a test string'; $f->validate($str); // false $f->filter($str); // 'This '
Filterus有很多内建模式,支持链式用法,甚至可以用独立的验证规则去验证数组元素。
16. Faker – 假数据生成器
Faker 是一个为你生成假数据的PHP库。当你需要填充一个测试数据库,或为你的web应用生成测试数据时,它能派上用场。它也非常容易使用:
//引用Faker 自动加载器 require_once '/path/to/Faker/src/autoload.php'; //使用工厂创建来创建一个Faker/Generator实例 $faker = Faker/Factory::create(); //通过访问属性生成假数据 echo $faker->name; // 'Lucy Cechtelar'; echo $faker->address; // "426 Jordy Lodge // Cartwrightshire, SC 88120-6700" echo $faker->text; // Sint velit eveniet. Rerum atque repellat voluptatem quia ...
只要你继续访问对象属性,它将继续返回随机生成的数据。
17. Mustache.php – 优雅模板库
Mustache是一款流行的模板语言,实际已经在各种编程语言中得到实现。使用它,你可以在客户端或服务段重用模板。 正如你猜得那样,Mustache.php 是使用PHP实现的。
$m = new Mustache_Engine; echo $m->render('Hello {{planet}}', array('planet' => 'World!')); // "Hello World!"
建议看一下官方网站Mustache docs 查看更多高级的例子。
18. Gaufrette – 文件系统抽象层
Gaufrette是一个PHP5库,提供了一个文件系统的抽象层。它使得以相同方式操控本地文件,FTP服务器,亚马逊 S3或更多操作变为可能。它允许你开发程序时,不用了解未来你将怎么访问你的文件。
use Gaufrette/Filesystem; use Gaufrette/Adapter/Ftp as FtpAdapter; use Gaufrette/Adapter/Local as LocalAdapter; //本地文件: $adapter = new LocalAdapter('/var/media'); //可选地使用一个FTP适配器 // $ftp = new FtpAdapter($path, $host, $username, $password, $port); //初始化文件系统 $filesystem = new Filesystem($adapter); //使用它 $content = $filesystem->read('myFile'); $content = 'Hello I am the new content'; $filesystem->write('myFile', $content);
也有缓存和内存适配器,并且随后将会增加更多适配器。
19. Omnipay – 支付处理库
Omnipay是一个PHP支付处理库。它有一个清晰一致的API,并且支持数十个网关。使用这个库,你仅仅需要学习一个API和处理各种各样的支付处理器。下面是一个例子:
use Omnipay/CreditCard; use Omnipay/GatewayFactory; $gateway = GatewayFactory::create('Stripe'); $gateway->setApiKey('abc123'); $formData = ['number' => '4111111111111111', 'expiryMonth' => 6, 'expiryYear' => 2016]; $response = $gateway->purchase(['amount' => 1000, 'card' => $formData]); if ($response->isSuccessful()) { //支付成功:更新数据库 print_r($response); } elseif ($response->isRedirect()) { //跳转到异地支付网关 $response->redirect(); } else { //支付失败:向客户显示信息 exit($response->getMessage()); }
使用相同一致的API,可以很容易地支持多种支付处理器,或在需要时进行切换。
20. Upload – 处理文件上传
Upload是一个简化文件上传和验证的库。上传表单时,这个库会校验文件类型和尺寸。
$storage = new /Upload/Storage/FileSystem('/path/to/directory'); $file = new /Upload/File('foo', $storage); //验证文件上传 $file->addValidations(array( //确保文件类型是"image/png" new /Upload/Validation/Mimetype('image/png'), //确保文件不超过5M(使用"B","K","M"或者"G") new /Upload/Validation/Size('5M') )); //试图上传文件 try { //成功 $file->upload(); } catch (/Exception $e) { //失败! $errors = $file->getErrors(); }
它将减少不少乏味的代码。
21. HTMLPurifier – HTML XSS 防护
HTMLPurifier是一个HTML过滤库,通过强大的白名单和聚集分析,保护你代码远离XSS攻击。它也确保输出标记符合标准。 (源码在github上)
require_once '/path/to/HTMLPurifier.auto.php'; $config = HTMLPurifier_Config::createDefault(); $purifier = new HTMLPurifier($config); $clean_html = $purifier->purify($dirty_html);
如果你的网站允许用户提交 HTML 代码,不修改就展示代码的话,那这时候就是用这个库的时候了。
22. ColorJizz-PHP – 颜色操控库
ColorJizz是一个简单的库,借助它你可以转换不同的颜色格式,并且做简单的颜色运算
use MischiefCollective/ColorJizz/Formats/Hex; $red_hex = new Hex(0xFF0000); $red_cmyk = $hex->toCMYK(); echo $red_cmyk; // 0,1,1,0 echo Hex::fromString('red')->hue(-20)->greyscale(); // 555555
它已经支持并且可以操控所有主流颜色格式了
23. PHP Geo – 地理位置定位库
phpgeo是一个简单的库,用于计算地理坐标之间高精度距离。例如:
use Location/Coordinate; use Location/Distance/Vincenty; $coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit 茂纳凯亚峰 $coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit $calculator = new Vincenty(); $distance = $calculator->getDistance($coordinate1, $coordinate2); // returns 128130.850 (meters; ≈128 kilometers)
它将在使用地理位置数据的app里出色工作。你可以试译 HTML5 Location API,雅虎的API(或两者都用,我们在weather web app tutorial中这样做了),来获取坐标。
24. ShellWrap – 优美的命令行包装器
借助 ShellWrap 库,你可以在PHP代码里使用强大的 Linux/Unix 命令行工具。
require 'ShellWrap.php'; use /MrRio/ShellWrap as sh; //列出当前文件下的所有文件 echo sh::ls(); //检出一个git分支 sh::git('checkout', 'master'); //你也可以通过管道把一个命令的输出用户另一个命令 //下面通过curl跟踪位置,然后通过grep过滤’html’管道来下载example.com网站 echo sh::grep('html', sh::curl('http://example.com', array( 'location' => true ))); //新建一个文件 sh::touch('file.html'); //移除文件 sh::rm('file.html'); //再次移除文件(这次失败了,然后因为文件不存在而抛出异常) try { sh::rm('file.html'); } catch (Exception $e) { echo 'Caught failing sh::rm() call'; }
当命令行里发生异常时,这个库抛出异常,所以你可以及时对之做出反应。它也可以通过管道让你一个命令的输出作为另一个命令的输入,来实现更强的灵活性。
以上就介绍了24 个你应该了解的 PHP 库,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。