码迷,mamicode.com
首页 > Web开发 > 详细

各种实用的 PHP 开源库推荐【转】

时间:2017-04-30 20:36:53      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:缓存   option   filter   nod   print   适用于   factory   cpu   edit   

 

转自: https://my.oschina.net/editorial-story/blog/882780

 

PHP 是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,利于学习,使用广泛,主要适用于 Web 开发领域,是大多数后端开发者的首选。PHP 作为最受欢迎的编程语言之一,经常出现在各大语言之战中,但到底谁是最好的编程语言呢?这不是文章要讨论的内容:)

本文从众多 PHP 开源库中选出了几款实用有趣的工具,希望对你的学习工作有帮助。

1、PHP 日志工具 Monolog

Monolog 是一种支持 PHP 5.3+ 以上的日志记录工具。并为 Symfony2 默认支持。

示例代码:

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger(‘name‘);
$log->pushHandler(new StreamHandler(‘path/to/your.log‘, Logger::WARNING));

// add records to the log
$log->addWarning(‘Foo‘);
$log->addError(‘Bar‘);

2、Excel 操作库 PHPExcel

PHPExcel 是一个用来读写 Excel 2007 (OpenXML) 文件的 PHP 库。

示例代码:

include ‘PHPExcel/IOFactory.php‘;

$inputFileName = ‘./sampleData/example1.xls‘;

echo ‘Loading file ‘,pathinfo($inputFileName,PATHINFO_BASENAME),‘ using IOFactory‘;
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);

3、PHP 机器学习库 PHP-ML 

PHP-ml 是 PHP 的机器学习库。同时包含算法,交叉验证,神经网络,预处理,特征提取等。

示例代码:

use Phpml\Classification\KNearestNeighbors;

$samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
$labels = [‘a‘, ‘a‘, ‘a‘, ‘b‘, ‘b‘, ‘b‘];

$classifier = new KNearestNeighbors();
$classifier->train($samples, $labels);

$classifier->predict([3, 2]); 
// return ‘b‘

4、PHP 的 OAuth 库 Opauth

Opauth 是一个开源的 PHP 库,提供了 OAuth 认证的支持,让你无需关注不同 Provider 之间的差别,提供统一标准的访问方法。目前支持 Google、Twitter 和 Facebook,其他的 Provider 支持也将陆续提供。同时也支持处理任何 OAuth 认证提供方。

技术分享

5、PHP 调试库 Whoops

Whoops 适用于PHP环境的错误捕获与调试PHP库; whoops非常容易使用,它提供stack-based错误捕获及超美观的错误查看。

技术分享

6、PHP 缓存库 phpFastCache 

phpFastCache 是一个开源的 PHP 缓存库,只提供一个简单的 PHP 文件,可方便集成到已有项目,支持多种缓存方法,包括:apc, memcache, memcached, wincache, files, pdo and mpdo。可通过简单的 API 来定义缓存的有效时间。

示例代码:

<?php
    // In your config file
    include("php_fast_cache.php");
    // This is Optional Config only. You can skip these lines.
    // phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "pdo", "mpdo" and "xcache"
    // You don‘t need to change your code when you change your caching system. Or simple keep it auto
    phpFastCache::$storage = "auto";
    // End Optionals

    // In your Class, Functions, PHP Pages
    // try to get from Cache first.
    $products = phpFastCache::get("products_page");

    if($products == null) {
        $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;
        // set products in to cache in 600 seconds = 10 minutes
        phpFastCache::set("products_page",$products,600);
    }

    foreach($products as $product) {
        // Output Your Contents HERE
    }
?>

7、PHP 框架 Guzzle

Guzzle 是个 PHP 框架,解决了发送大量 HTTP 请求和创建 web 服务客户端的问题。它包括了创建坚实服务客户端的工具,包括:服务描述来定义 API 的输入和输出,通过分页资源实现资源迭代,尽可能高效的批量发送大量的请求。

示例代码:

$client = new GuzzleHttp\Client();
$res = $client->request(‘GET‘, ‘https://api.github.com/user‘, [
    ‘auth‘ => [‘user‘, ‘pass‘]
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader(‘content-type‘);
// ‘application/json; charset=utf8‘
echo $res->getBody();
// {"type":"User"...‘

// Send an asynchronous request.
$request = new \GuzzleHttp\Psr7\Request(‘GET‘, ‘http://httpbin.org‘);
$promise = $client->sendAsync($request)->then(function ($response) {
    echo ‘I completed! ‘ . $response->getBody();
});
$promise->wait();

8、CSS-JS合并/压缩 Munee

Munee是一个集图片尺寸调整、CSS-JS合并/压缩、缓存等功能于一身的PHP库。可以在服务器端和客户端缓存资源。它集成了PHP图片操作库Imagine来实现图片尺寸调整和剪切,之后进行缓存。

示例代码:

require ‘vendor/autoload.php‘;
echo \Munee\Dispatcher::run(new \Munee\Request());
<!-- Combining two CSS files into one. -->
<link rel="stylesheet" href="/css/bootstrap.min.css, /css/demo.css">

<!-- Resizing image -->
<img src="/path/to/image.jpg?resize=width[100]height[100]exact[true]">

<!-- Files that need preprocessing are compiled automatically -->
<link rel="stylesheet" href="/css/demo.scss">

<!-- Minifying code -->
<script src="/js/script.js?minify=true"></script>

9、PHP 模板语言 Twig

Twig是一个灵活,快速,安全的PHP模板语言。它将模板编译成经过优化的原始PHP代码。Twig拥有一个Sandbox模型来检测不可信的模板代码。Twig由一个灵活的词法分析器和语法分析器组成,可以让开发人员定义自己的标签,过滤器并创建自己的DSL。

示例代码:

// Template HTML

<p>Welcome {{ name }}!</p>


// Rendering

require_once ‘/path/to/lib/Twig/Autoloader.php‘;
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem(‘/path/to/templates‘);
$twig = new Twig_Environment($loader, array(
    ‘cache‘ => ‘/path/to/compilation_cache‘,
));

echo $twig->render(‘index.html‘, array(‘name‘ => ‘George‘));

10、PHP 爬虫库 Goutte

Goutte 是一个抓取网站数据的 PHP 库。它提供了一个优雅的 API,这使得从远程页面上选择特定元素变得简单。

示例代码:

require_once ‘/path/to/goutte.phar‘;

use Goutte\Client;

//发送请求
$client = new Client();
$crawler = $client->request(‘GET‘, ‘http://www.oschina.net/‘);

//点击链接
$link = $crawler->selectLink(‘Plugins‘)->link();
$crawler = $client->click($link);

//提交表单
$form = $crawler->selectButton(‘sign in‘)->form();
$crawler = $client->submit($form, array(‘signin[username]‘ => ‘fabien‘, ‘signin[password]‘ => ‘xxxxxx‘));

//提取数据
$nodes = $crawler->filter(‘.error_list‘);
if ($nodes->count())
{
  die(sprintf("Authentication error: %s\n", $nodes->text()));
}

printf("Nb tasks: %d\n", $crawler->filter(‘#nb_tasks‘)->text());

 

11、PHP 邮件发送包 PHPMailer

PHPMailer是一个用于发送电子邮件的PHP函数包。它提供的功能包括:

  • 在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址
  • 支持多种邮件编码包括:8bit,base64,binary和quoted-printable
  • 支持SMTP验证
  • 支持冗余SMTP服务器
  • 支持带附件的邮件和Html格式的邮件
  • 自定义邮件头
  • 支持在邮件中嵌入图片
  • 调试灵活
  • 经测试兼容的SMTP服务器包括:Sendmail,qmail,Postfix,Imail,Exchange等
  • 可运行在任何平台之上

12、PHP 图表制作 pChart

pChart是一个基于GD library(图形处理函数库)开发的PHP图表制作开源项目。支持多种图表类型包括:

  • Line chart

  • Cubic curve chart

  • Plot chart

  • Bar chart

  • Filled line chart

  • Filled cubic curve chart

  • Pie chart

  • Radars chart

  • Limits chart

13、PHP 快速开发类库 Eden 

Eden是一个开源且免费的PHP快速开发类库。它包含很多组件用来自动加载、事件驱动、文档系统、缓存、模板、国际化、数据库、web服务、支付网关、装载和云服务技术。

技术分享

14、PHP 生成 PDF 的类 FPDF

FPDF这个PHP Class允许你采用纯PHP(更确切地说就是不需要使用PDFlib)来生成PDF文件。

它所具有的特点包括:

  • 可选择的unit大小,页面格式和页边 距;
  • 页眉和页脚管理;
  • 自动分页;
  • 自动换行与文本自动对齐;
  • 支持JPEG与PNG图片格式;
  • 支持着色和文件超链接;
  • 支持TrueType,Type1与 encoding;
  • 支持页面压缩。

示例代码:

//Determine a temporary file name in the current directory
$file = basename(tempnam(‘.‘, ‘tmp‘));
rename($file, $file.‘.pdf‘);
$file .= ‘.pdf‘;
//Save PDF to file
$pdf->Output($file, ‘F‘);
//Redirect
header(‘Location: ‘.$file);

15、PHP Error

PHP Error 是一个开源的 PHP 库,用于转换标准的 PHP 错误信息,主要用于开发过程中的调试。PHP Error 紧密集成到 PHP 环境中,显示带语法高亮的错误提示。

技术分享

16、PHP 单元测试框架 SimpleTest

SimpleTest 是一个为PHP程序提供的单元测试的框架,包含一个内嵌的web浏览器用来测试PHP的Web网站。

示例代码:

 <?php
require_once(‘simpletest/unit_tester.php‘);
require_once(‘simpletest/reporter.php‘);
require_once(‘../classes/log.php‘);

class TestOfLogging extends UnitTestCase {
    
    function testCreatingNewFile() {
        @unlink(‘/temp/test.log‘);
        $log = new Log(‘/temp/test.log‘);
        $this->assertFalse(file_exists(‘/temp/test.log‘));
        $log->message(‘Should write this to a file‘);
        $this->assertTrue(file_exists(‘/temp/test.log‘));
    }
}

$test = &new TestOfLogging();
$test->run(new HtmlReporter());
?>

17、PHP 的 WebSockets 开发包 PHP Ratchet

Ratchet 是一个松耦合的 PHP 库,提供了用于创建实时、双向客户端服务器 WebSockets 应用的支持。

示例代码:

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    public function onOpen(ConnectionInterface $conn) {
    }

    public function onMessage(ConnectionInterface $from, $msg) {
    }

    public function onClose(ConnectionInterface $conn) {
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}

18、模块化 PHP 库集合 Hoa

Hoa 是模块化,可扩展和结构化的 PHP 库集合。Hoa 的目标是搭建工业和研究之间的桥梁。 

可以通过组合和扩展来 Hoa 创建自己的应用和库。

技术分享

 

各种实用的 PHP 开源库推荐【转】

标签:缓存   option   filter   nod   print   适用于   factory   cpu   edit   

原文地址:http://www.cnblogs.com/taozi32/p/6790226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!