24 Cool PHP Libraries You Should Know About

原文出處:http://tutorialzine.com/2013/02/24-cool-php-libraries-you-should-know-about/ 先Mark一下,之前再來慢慢看 😛

1. Dispatch – Micro Framework

Dispatch is a minimal PHP framework. It doesn’t give you the full MVC setup, but you can define URL rules and methods to better organize your application. This is perfect for APIs, simple sites or prototypes:

You can match specific types of HTTP requests and paths, render views and more. If you combine Dispatch with some of the other frameworks here, you can have a really powerful and lightweight setup!

2. Klein – Lightning fast router for PHP

Klein is another light weight routing library for PHP 5.3+. It has a bit more verbose syntax than Dispatch, but is quite fast. Here is an example:

You can also subscribe to specific HTTP methods and use regexes as paths:

This is great for small projects, but you have to be disciplined when using a library like this for larger apps, as your code can become unmaintainable very fast. For this purpose, you would be better off with a full blown MVC framework like Laravel or CodeIgniter.

3. Ham – Routing Library with Caching

Ham is also a lightweight routing framework but it utilizes caching for even more speed gains. It achieves this by caching anything I/O related in XCache/APC. Here is an example:

The library requires that you have either XCache or APC installed, which would mean that it won’t work on most hosting providers. But if you do have one of these installed or if you control your webserver, you should try this very fast framework.

4. Assetic – Asset Management

Assetic is am asset management framework for PHP. It combines and minifies your CSS/JS assets. Here is how it is used:

Combining assets in this manner is a good idea, as it can speed up your site. Not only is the total download size reduced, but also a lot of unnecessary HTTP requests are eliminated (two of the things that affect page load time the most).

5. ImageWorkshop – Image Manipulation with Layers

ImageWorkshop is an Open Source library that lets you manipulate images with layers. With it you can resize, crop, make thumbnails, add watermarks and more. Here is an example:

ImageWorkshop is developed to make easy the most common cases for manipulating images in PHP. If you need something more powerful though, you should look at the Imagine library.

6. Snappy – Snapshot/PDF Library

Snappy is a PHP5 library that allows you to take snapshots or PDFs of URLs or HTML documents. It depends on the wkhtmltopdf binary, which is available on Linux, Windows and OSX. You use it like this:

Keep in mind that calling external binaries might not be allowed by your hosting provider.

7. Idiorm – Lightweight ORM Library

Idiorm is a personal favorite that I have used in tutorials in this site before. It is a lightweight ORM library and a fluent query builder for PHP5 that is built on top of PDO. With it, you can forget writing tedious SQL:

Idiorm has a sister library called Paris, which is an Active Record implementation built on top of it.

8. Underscore – PHP’s Utility Belt

Underscore is a port of the original Underscore.js – the utility belt for JavaScript applications. The PHP version doesn’t disappoint and has support for nearly all of the original’s functionality. Some examples:

The library also has support for chaining, which makes it even more powerful.

9. Requests – Easy HTTP Requests

Requests is a library that makes it easy to issue HTTP requests. If you are like me, and can never seem to remember the various options passed to Curl, this is for you:

With this library, you can send HEAD, GET, POST, PUT, DELETE and PATCH HTTP requests, add files and parameters with arrays, and access all the response data.

10. Buzz – Simple HTTP Request Library

Buzz is another PHP library for issuing HTTP requests. Here is an example:

It is lacking in documentation, so you will have to read through the source code to get a feel of all the options that it supports. Or you can go with the Requests library that I presented above.

11. Goutte – Web Scraping Library

Goutte is a library for scraping websites and extracting data. It provides a nice API that makes it easy to select specific elements from the remote pages.

12. Carbon – DateTime Library

Carbon is a simple API extension for the DateTime. It enhances the class with some useful methods for working with dates and time. For example:

Carbon hasn’t seen new commits in the last few months, so you might want to check out Datum, a fork that continues to be worked on.

13. Ubench – Micro Benchmarking Library

Ubench is a micro library for benchmarking your PHP code. It monitors execution time and memory usage. Here’s an example:

It would be a good idea to run these checks only while developing.

14. Validation – Input Validation Engine

Validation claims to be the most awesome validation engine ever created for PHP. But can it deliver? See for yourself:

With this library you can validate your forms or other user-submitted data. In addition, it supports a wide number of existing checks, throwing exceptions and customizable error messages.

15. Filterus – Filtering Library

Filterus is another filtering library, but it can not only validate, but also filter input to match a preset pattern. Here is an example:

Filterus has a lot of built-in patterns, supports chaining and can even validate array elements with individual validation rules.

16. Faker – Fake Data Generator

Faker is a PHP library that generates fake data for you. It can come handy when you need to populate a test database or generate sample data for your web application. It is also very easy to use:

As long as you keep accessing properties of the object, it will continue returning randomly generated data.

17. Mustache.php – Elegant Templating Library

Mustache is a popular templating language that has implementations in practically every programming languages. This gives you the benefit that you can reuse your templates in both client and server side.Mustache.php is an implementation that uses – you guessed it – PHP:

To see more advanced examples, I suggest taking a look at the official Mustache docs.

18. Gaufrette – File System Abstraction Layer

Gaufrette is a PHP5 library that provides a filesystem abstraction layer. It makes it possible to work with local files, FTP servers, Amazon S3 and more in the same way. This permits you to develop your application without having to know how you are going to access your files in the future.

There are also caching and in-memory adapters, and more will be added over time.

19. Omnipay – Payment Processing Library

Omnipay is a payment processing library for PHP. It has a clear and consistent API and supports dozens of gateways. With this library, you only need to learn one API and work with a variety of payment processors. Here is an example:

Using the same consistent API makes it easy to support multiple payment processors or to switch as the need arises.

20. Upload – For Handling File Uploads

Upload is a library that simplifies file uploading and validation. When a form is submitted, the library can check the type of file and size:

This will save you lots of tedious code.

21. HTMLPurifier – HTML XSS Protection

HTMLPurifier (on github) is an HTML filtering library that protects your code from XSS attacks by using a combination of robust whitelists and agressive parsing. It also makes sure that the resulting markup is standards compliant.

The best place to use this library would be when you are allowing users to submit HTML which is to be displayed unmodified on the site.

22. ColorJizz-PHP – Color Manipulation Library

ColorJizz is a tiny library that can convert between different color formats and do simple color arithmetic. For example:

It has support for and can manipulate all major color formats.

23. PHP Geo – Geo Location Library

phpgeo is a simple library for calculating distances between geographic coordinates with high precision. For example:

This will work great in apps that make use of location data. To obtain the coordinates, you can use the HTML5 Location API, Yahoo’s API (or both, like we did in the weather web app tutorial).

24. ShellWrap – Beautiful Shell Wrapper

ShellWrap is library that allows you to work with the powerful Linux/Unix command line tools in PHP through a beautiful syntax:

The library throws exceptions when an error occurs in the command, so you can act accordingly. It also can pipe the output of one command as the input of another for even greater flexibility.


發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料