Skip to main content

composer组件和用法

1. monolog 日志

网址:https://github.com/Seldaek/monolog

命令:

$ composer require monolog/monolog
<?php
require 'vendor/autoload.php';

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

// 创建一个日志
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/ your.log', Logger::WARNING));

// 将记录添加到日志中
$log->warning('Foo');
$log->error('Bar');

2. starlit/db

网址:https://github.com/starweb/starlit-db

$ composer require starlit/db

测试:

<?php
require 'vendor/autoload.php';

use Starlit\Db\Db;

$db = new Db('localhost', 'zhangsan', '123', 'mydb');
//echo $db->insert('stu', ['id'=>null,'name' => 'zhangwuji','age'=>20]);
$res = $db->fetchRows("select * from stu where id=:id",['id'=>2]);
echo "<pre>";
print_r($res);

3. 验证码

安装命令:

composer require gregwar/captcha

在composer.json中添加:

"require": {
...
...
"gregwar/captcha": "1.*"
},

具体使用

<?php
require 'vendor/autoload.php';

use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

$phrase = new PhraseBuilder();
// 设置验证码位数
$code = $phrase->build(6);
// 生成验证码图片的Builder对象,配置相应属性
$builder = new CaptchaBuilder($code, $phrase);
// 设置背景颜色
$builder->setBackgroundColor(220, 210, 230);
$builder->setMaxAngle(25);
$builder->setMaxBehindLines(0);
$builder->setMaxFrontLines(0);
// 可以设置图片宽高及字体
$builder->build($width = 100, $height = 40, $font = null);
// 获取验证码的内容
$phrase = $builder->getPhrase();
// 把内容存入session
//\Session::flash('code', $phrase);
// 生成图片
header("Cache-Control: no-cache, must-revalidate");
header("Content-Type:image/jpeg");
$builder->output();

4. 基于词库的中文转拼音优质解决方案 overtrue/pinyin

网址:https://github.com/overtrue/pinyin

composer require "overtrue/pinyin:~3.0"
<?php
require 'vendor/autoload.php';

use Overtrue\Pinyin\Pinyin;

$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"]

5. twbs/Bootstrap

网址:https://github.com/twbs/bootstrap

composer require twbs/bootstrap

6. Composer的endroid/Qrcode二维码组件

网址:https://github.com/endroid/QrCode

安装命令:

$ composer require endroid/qrcode

具体使用:

<?php
use Endroid\QrCode\QrCode;

$qrCode = new QrCode();
$qrCode->setText('Life is too short to be generating QR codes')
->setSize(300)
->setPadding(10)
->setErrorCorrection('high')
->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
->setLabel('Scan the code')
->setLabelFontSize(16)
->setImageType(QrCode::IMAGE_TYPE_PNG);

// now we can directly output the qrcode
header('Content-Type: '.$qrCode->getContentType());
$qrCode->render();

// or create a response object
$response = new Response($qrCode->get(), 200, array('Content-Type' => $qrCode->getContentType()));

7. Guzzle HTTP客户端组件

网址:https://github.com/guzzle/guzzle
官方网站: http://guzzlephp.org/
环境要求
>=PHP5.5
在php.ini中开启 allow_url_fopen
cURL >= 7.19.4 并支持OpenSSL和zlib

安装命令:

$ composer require guzzlehttp/guzzle:~6.0

参考实例代码:

创建一个客户端

<?php
setCookie("name","lisi",time()+3600,"/");
require("../vendor/autoload.php");

use GuzzleHttp\Client;

$client = new Client([
// 基本URI用于相对请求
'base_uri' => 'http://192.168.33.10',
//你可以设置任意数量的默认值
'timeout' => 2.0,
//'cookies' => $jar,
]);

//GET请求及带参数
$response = $client->request("GET","/mydemo/public/1.php?id=10");
//$response = $client->request("GET","/mydemo/public/1.php",['query'=>["name"=>"zhangsan","age"=>20]]);
//$response = $client->get("/mydemo/public/1.php",['query'=>["name"=>"zhangsan","age"=>20]]);

//POST请求并带参数
//$response = $client->request("POST","/mydemo/public/1.php",['form_params'=>["name"=>"zhangsan","age"=>20]]);
//$response = $client->post("/mydemo/public/1.php",['form_params'=>["name"=>"zhangsan","age"=>20]]);

/*
//带上传信息
$response = $client->request('POST', '/mydemo/public/1.php',['multipart'=>[[
'name' => 'file_name',
'contents' => fopen('./1.png', 'r'),
'filename' => '1.png',
]]]);
*/

//携带Cookie信息
//$jar = \GuzzleHttp\Cookie\CookieJar::fromArray(['name'=>"zhangsan"],"192.168.33.10");
//$response = $client->request('GET', '/mydemo/public/1.php',['cookies'=>$jar]);


//处理相应
//var_dump($response);
/*
echo "code:".$response->getStatusCode()."<br/>"; //获取响应状态码200
echo "reason:".$response->getReasonPhrase()."<br/>"; //响应是否成功OK

//判断响应头是否存在
if ($response->hasHeader('Content-Length')) {
echo "It exists";
}
//获取指定某个响应头信息
echo $response->getHeader('Content-Length');
//获取所有响应头信息
foreach ($response->getHeaders() as $name => $values) {
echo $name . ': ' . implode(', ', $values) . "\r\n";
}
*/
$body = $response->getBody();
// 隐式地将主体转换为字符串并对其进行响应
//var_dump($body);
//echo $body;
//显式地将主体转换为字符串
//$stringBody = (string) $body;
// 从正文中读取10个字节
//echo $tenBytes = $body->read(5);
// 将主体的剩余内容作为字符串读取
echo $remainingBytes = $body->getContents();