PHP TP5 yii2.0 larval5.4 引入外部扩展教程
总结一下 , 最近突然想起来, 长时间不用就忘掉了 ,今天有点时间 就把记录下来了 TP 5.0
扩展存放路径
API 是写的一个提供交互的API扩展
<?php
/**
* @公共文件
*/
class Api
{
static function get($path="api/index")
{
$ip = "10.".rand(2,90).".".rand(2,90).".".rand(2,150);//随机IP
$token = "FDodamasddsadasdsfgsdfddasa";//秘钥
$time = time(); //时间
$sign = sha1($token.$time.$path);//s生成加密
$path = $path."&fdtime=".$time."&fdopen=".$sign;
$str = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
$location = @strrpos($str,"admin/public/");
$str = @substr($str,0,$location).'api/web/index.php?r='.$path;
$HTTP_REQUEST_HEADER = array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding: gzip, deflate",
"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control: no-cache",
"CLIENT-IP: ".$ip,
"X-FORWARDED-FOR: ".$ip,
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
);
$list =curl_init();
curl_setopt($list, CURLOPT_URL, $str); //请求地址
curl_setopt($list, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");//模拟浏览器
curl_setopt($list, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($list, CURLOPT_FOLLOWLOCATION, false); //是否抓取跳转后的页面,1是自动跳转
curl_setopt($list, CURLOPT_HTTPHEADER, $HTTP_REQUEST_HEADER);
curl_setopt($list, CURLOPT_ACCEPT_ENCODING, "gzip, deflate, sdch");
curl_setopt($list, CURLOPT_SSL_VERIFYPEER, FALSE);//对认证证书来源的检查,不开启次功能
curl_setopt($list, CURLOPT_SSL_VERIFYHOST, 2); //从证书中检测 SSL 加密算法.3
$res = curl_exec($list);
curl_close($list);
return $res;
}
static function post($path="api/index",$data=array())
{ $ip = "10.".rand(2,90).".".rand(2,90).".".rand(2,150);//随机IP
$token = "FDodamasddsadasdsfgsdfddasa";//秘钥
$time = time(); //时间
$sign = sha1($token.$time.$path);//s生成加密
$path = $path."&fdtime=".$time."&fdopen=".$sign;
$str = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
$location = @strrpos($str,"home/public");
$str = @substr($str,0,$location).'api/web/index.php?r='.$path;
// echo "$str";die();
$HTTP_REQUEST_HEADER = array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding: gzip, deflate",
"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control: no-cache",
"CLIENT-IP: ".$ip,
"X-FORWARDED-FOR: ".$ip,
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
);
$list =curl_init();
curl_setopt($list, CURLOPT_URL, $str); //请求地址
curl_setopt($list, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");//模拟浏览器
curl_setopt($list, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($list, CURLOPT_FOLLOWLOCATION, false); //是否抓取跳转后的页面,1是自动跳转
curl_setopt($list, CURLOPT_HTTPHEADER, $HTTP_REQUEST_HEADER);
curl_setopt($list, CURLOPT_ACCEPT_ENCODING, "gzip, deflate, sdch");
curl_setopt($list, CURLOPT_SSL_VERIFYPEER, FALSE);//对认证证书来源的检查,不开启次功能
curl_setopt($list, CURLOPT_SSL_VERIFYHOST, 2); //从证书中检测 SSL 加密算法.3
curl_setopt($list,CURLOPT_POST,1);
curl_setopt($list,CURLOPT_POSTFIELDS,$data);
$res = curl_exec($list);
curl_close($list);
return $res;
}
public function Checked(){//数据来源验证
$data = isset($_SERVER['QUERY_STRING'])?$_SERVER['QUERY_STRING']:' ';
$data = @explode("&",$data);
$ar = array();
foreach ($data as $key => $value) {
$datas = @explode("=",$value);
if ($datas['0']=="fdtime"||$datas['0']=="fdopen"||$datas['0']=="r") {
$ar[$datas['0']]=$datas['1'];
}
}
if (count($ar)==3) {
$token = "FDodamasddsadasdsfgsdfddasa";//秘钥
$time = $ar['fdtime']; //时间
$path = $ar['r'];
$sign = sha1($token.$time.$path);//s生成加密
if ($sign==$ar['fdopen']) {
// return true;
}else{
Header("HTTP/1.1 404 Not Found");
echo json_encode(array('content'=>'错误请求来源','code'=>10001)) ;exit();
}
}else{
Header("HTTP/1.1 404 Not Found");
echo json_encode(array('content'=>'错误请求来源','code'=>10001)) ;exit();
}
}
}
?>
C控制器调用
import(‘类库名’, ‘起始路径’, ‘类库后缀’)
imprt方法有一个别名vendor方法,专门用于导入第三方类库
写的是一个API 的接口 调用
<?php
namespace app\index\controller;
// use ;//调用接口 Api::post('API接口地址',数据);
use think\Controller;
use think\Request;
use think\Db;
use think\Loader;//引入加载库
class Index
{
public function index()
{
import('mail.Api');//import方法是ThinkPHP框架用于类库导入的封装实现
Loader::import('mail/Api', EXTEND_PATH);
echo \Api::post('接口地址','参数');
}
}
YII2.0
yii2 放的是一个邮箱扩展 负责发送邮箱
<?php
namespace app\mail\email;
use yii\web\Controller;
use Yii;
class SEND_email
{
static public function sendMail(){
////content 这里是内容
}
}
控制器调用
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\mail\email\SEND_email;//邮箱 扩展
class LoginController extends Controller
{
public function actionApi(){
SEND_email::sendMail
}
}
larval5.4
扩展目录
Api.php 内容
<?php
namespace app\mail;
/**
* @公共文件
*/
class Api
{
static function get($path="api/index")
{
//内容
}
}
?>
进入
vendor\composer\autoload_classmap.php 在文件里面添加
'APP\\mail\\Api' => $baseDir . '/app/mail/Api.php',
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\SessionController;
use App\mail\Api;//调用接口
class LoginController extends Controller
{
public function sign_in(Request $request){/
//调用
$data = Api::get('login/login_in',['user_name'=>$user_name,'user_pwd'=>$user_pwd]);
}
}
|
请发表评论