PHPer Initiative Learning Road: Recommendations for Practical PHP Open Source Libraries (1)

PHP is a universal open source scripting language. Grammar absorbs the characteristics of C language, Java and Perl. It is easy to learn and widely used. It is mainly applicable to the field of Web development and is the first choice of most back-end developers. As one of the most popular programming languages, PHP often appears in the battle of major languages, but who is the best programming language? This is not what the article is about.

This article selected several useful and interesting tools from many open source PHP libraries, hoping to be helpful to your study and work.

1,PHP Logging Tool Monolog

Monolog is a logging tool that supports PHP 5.3 + or more. For the sake of ____________ Symfony 2 Default support.

Sample code:

<?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 Operating Library PHPExcel

PHPExcel is a PHP library for reading and writing Excel 2007 (OpenXML) files.

Sample code:

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 Machine Learning Library PHP-ML

PHP-ml is the machine learning library of PHP. At the same time, it includes algorithm, cross validation, neural network, preprocessing, feature extraction and so on.

Sample code:

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's OAuth Library Opauth

Opauth is an open source PHP library that provides OAuth authentication support so that you don't need to pay attention to the differences between different providers and provide a unified standard access method. Google, Twitter and Facebook are currently supported, and other Provider support will be provided one after another. It also supports the processing of any OAuth authentication provider.

5,PHP Debugging Library Whoops

Whoops is suitable for error capture and debugging PHP libraries in PHP environments; whoops is very easy to use and provides stack-based error capture and super-beautiful error viewing.

6,PHP Cache

phpFastCache is an open source PHP cache library. It only provides a simple PHP file, which can be easily integrated into existing projects. It supports a variety of caching methods, including apc, memcache, memcached, wincache, files, pdo and mpdo. A simple API can be used to define the effective time of the cache.

Sample code:

<?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 Framework Guzzle

Guzzle is a PHP framework that solves the problem of sending a large number of HTTP requests and creating web service clients. It includes tools to create solid service clients, including: service description to define the input and output of API, resource iteration through paging resources, and mass sending of requests as efficiently as possible.

Sample code:

$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 Merge/Compress Munee

Munee is a PHP library which integrates image size adjustment, CSS-JS merge/compression, caching and other functions. Resources can be cached on both server and client sides. It integrates PHP image manipulation library Imagine to realize image size adjustment and clipping, and then caches.

Sample code:

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 Template Language Twig

Twig is a flexible, fast and secure PHP template language. It compiles templates into optimized raw PHP code. Twig has a Sandbox model to detect untrustworthy template code. Twig consists of a flexible lexical analyzer and a parser that allows developers to define their own tags, filters and create their own DSL.

Sample code:

// 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 reptile library Goutte

Goutte is a PHP library that grabs website data. It provides an elegant API that makes it easy to select specific elements from remote pages.

Sample code:

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

use Goutte\Client;

//Send requests
$client = new Client();
$crawler = $client->request('GET', 'http://www.oschina.net/');

//Click on the link
$link = $crawler->selectLink('Plugins')->link();
$crawler = $client->click($link);

//Submit Form
$form = $crawler->selectButton('sign in')->form();
$crawler = $client->submit($form, array('signin[username]' => 'fabien', 'signin[password]' => 'xxxxxx'));

//Extraction of data
$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());

Responsible Editors: Open Source China - Darwin

For reprinting, please indicate the source and author of the article.

Keywords: PHP Programming Excel PDO

Added by bschmitt78 on Mon, 08 Jul 2019 06:58:31 +0300