Eight Useful PHP Libraries from GitHub to Boost Development Efficiency
This article presents eight highly starred PHP libraries from GitHub—including tools for pinyin conversion, cURL requests, Markdown parsing, HTML‑to‑PDF conversion, document handling, SMS sending, short‑URL generation, and console applications—along with code examples, and concludes with a promotion for an upcoming PHP online training class.
PHP中文网为同学们整理了 GitHub 上 Star 较高的 8 个实用的 PHP 库,提升开发效率。
1. pinyin (中文转拼音工具)
项目地址: https://github.com/overtrue/pinyin 。基于 CC-CEDICT 词典的中文转拼音工具,更准确地支持多音字。示例代码:
use OvertruePinyinPinyin;
$pinyin = new Pinyin();
$pinyin->convert('带着希望去旅行,比到达终点更美好');
// ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]
$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_UNICODE);
// ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]
$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCII);
//["dai4","zhe","xi1","wang4","qu4","lv3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]2. php-curl-class (PHP cURL 库)
项目地址: https://github.com/php-curl-class/php-curl-class 。封装了 PHP 的 cURL,使发送 HTTP 请求更加简洁,适用于爬虫或模拟访问场景。示例代码:
<?php
// 获取豆瓣电影示例
require '../vendor/autoload.php';
use Curl\Curl;
$curl = new Curl();
$url = "https://movie.douban.com/j/search_subjects?type=movie&tag=豆瓣高分&sort=time&page_limit=20&page_start=1";
$curl->get($url);
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
$curl->close();
var_dump($curl->getResponse());
exit;3. parsedown (Markdown 解析库)
项目地址: https://github.com/erusev/parsedown 。一个小而美的 PHP Markdown 解析库,能够将标准 Markdown 文本转换为 HTML,依赖少且自带完整单元测试。示例代码:
$Parsedown = new Parsedown();
echo $Parsedown->text('Hello _Parsedown_!'); // prints: <p>Hello <em>Parsedown</em>!</p>4. dompdf (HTML 转 PDF)
项目地址: https://github.com/dompdf/dompdf 。将 HTML 内容渲染为 PDF 的 PHP 库。示例代码:
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();5. PHPWord (文档读写库)
项目地址: https://github.com/PHPOffice/PHPWord 。提供对 Microsoft Office、RTF 等多种文档格式的读写支持。示例代码:
<?php
require_once 'bootstrap.php';
// 新建文档
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('"Learn from yesterday, live for today, hope for tomorrow. ' .
'The important thing is not to stop questioning." (Albert Einstein)');6. easy-sms (短信发送组件)
项目地址: https://github.com/overtrue/easy-sms 。支持多家短信服务商,提供统一的发送接口和灵活的策略配置。示例代码:
use Overtrue\EasySms\EasySms;
$config = [
'timeout' => 5.0,
'default' => [
'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
'gateways' => ['yunpian', 'aliyun'],
],
'gateways' => [
'errorlog' => ['file' => '/tmp/easy-sms.log'],
'yunpian' => ['api_key' => '824f0ff2f71cab52936axxxxxxxxxx'],
'aliyun' => [
'access_key_id' => '',
'access_key_secret' => '',
'sign_name' => '',
],
// ...
],
];
$easySms = new EasySms($config);
$easySms->send(13188888888, [
'content' => '您的验证码为: 6379',
'template' => 'SMS_001',
'data' => ['code' => 6379],
]);7. YOURLS (短网址生成)
项目地址: https://github.com/YOURLS/YOURLS 。完全免费且开源的短网址服务,支持数据统计、地理位置和可视化等功能,可自行部署。
8. php-console (PHP 命令行应用库)
项目地址: https://github.com/inhere/php-console 。提供控制台参数解析、命令运行、颜色输出和交互等功能,使用简单、功能全面。
此外,文章还宣布 PHP 中文网第 22 期线上直播班正式开始报名,提供前 50 名赠送实战课程、前 10 名赠送完整 PHP 课程等限额优惠。报名方式包括 QQ(27220243,钟老师)和微信(phpcn01,月月老师),并提供在线咨询二维码。
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.