PHP code example of smileyi / utils
1. Go to this page and download the library: Download smileyi/utils library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
smileyi / utils example snippets
use SmileYi\Ytil\Http;
use SmileYi\Ytil\Exception;
$url = 'http://192.144.150.100/api.php?method=get&task_id=1001';
$header = [
'Content-type' => 'application/json',
'User-agent' => 'utils/http'
];
$body = [
'param1' => 'value1'
];
//代理配置
//$proxy = 'http://xxx.xxx.xxx.xxx:8080'
$proxy = null;
//证书配置
// $cert = [
// 'ssl_cert' => 'path',
// 'ssl_key' => 'path',
// 'ca_info' => 'path'
// ];
$cert = null;
try {
//GET调用
$result = Http::get($url, $header, $proxy, $cert);
var_dump($result);
//POST调用
$result = Http::post($url, $body, $header, $proxy, $cert);
var_dump($result);
//并行调用
$multi = [];
$multi['name1'] = [
'method' => Http::METHOD_GET,
'url' => $url,
'header' => $header
];
$multi['name2'] = [
'method' => Http::METHOD_POST,
'url' => $url,
'header' => $header,
'body' => $body
];
$result = Http::multi($multi);
var_dump($result);
}catch(Exception $e){
echo "Errno:" . $e->getCode() . " Error:" . $e->getMessage()."\n";
}
use SmileYi\Ytil\Common;
# 加密解密
$t = 'hahaha';
$dt = Common::encrypt($t);
$et = Common::decrypt($dt);
echo "secret text:" . $dt . "\n";
echo "text:" . $et . "\n\n";
# 耗时计算
Common::exeTime('_start');
sleep(0.1);
Common::exeTime('_end');
echo "Exe time is:" . Common::exeTime('_start', '_end') . "s\n\n";
# 获取随机长度字符串
echo "random string is:" . Common::randStr(10) . "\n\n";
# 获取客户端IP
echo "client ip is:" . Common::getClientIp() . "\n\n";
use SmileYi\Ytil\Base64;
$text = 'smileyi';
# 编码
$dt = Base64::encode($text);
# 解码
$et = Base64::decode($dt);
echo "text base64 encode is:" . $dt . "\n";
echo "text is:" . $et . "\n\n";