公众平台配置地址
http://www.xiaoshu168.com/api/wechat?token=xiaoshu
token : xiaoshu
路由:
Route::any('wechat', 'WxApiController@index');控制器:
<?php
namespace App\Http\Controllers\Wxapi;
use App\Http\Model\Source\Wx\Wxtoken;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
class WxApiController extends Controller
{
/**
* @return mixed
* 请求入口
*/
public function index( Request $request )
{
$token = $request->input('token');
if( $request->method() == "GET" || $request->method() == "get" )
{
$res = Wxtoken::where('token',$token)->first();
if( $res )
{
$echoStr = $request->input('echostr');
if( $this->checkSignature( $request,$token ) )
{
echo $echoStr;
exit;
}
}
}else
{
$this->responseMsg();
}
}
/**
* @param $request
* @param $token
* @return bool.
* 验证
*/
private function checkSignature( $request,$token )
{
$signature = $request->input('signature');
$timestamp = $request->input('timestamp');
$nonce = $request->input('nonce');
$token = $token;
$tmpArr = array($token, $timestamp, $nonce );
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature )
{
return true;
}else
{
return false;
}
}
/**
* 请求
*/
public function responseMsg()
{
$postStr = file_get_contents("php://input");
if (!empty($postStr))
{
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$MsgType = trim($postObj->MsgType);
switch ( $MsgType )
{
case 'text':
echo $this->returnText( $postObj );
break;
}
}else
{
return '请求失败。。';
}
}
/**
* @param $postObj
* @return mixed
* 处理text类型
*/
private function returnText( $postObj )
{
$time = time();
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$keyword = trim($postObj->Content);
$url = 'http://www.tuling123.com/openapi/api?key=3382804569e14b1961d097353184218e&text='.$keyword;
$res = @file_get_contents($url);
$contentStr = isset(json_decode($res)->text)?json_decode($res)->text:'不知道你要干嘛了。。。';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
return $resultStr;
}
}模型:
<?php
namespace App\Http\Model\Source\Wx;
use Illuminate\Database\Eloquent\Model;
class Wxtoken extends Model
{
protected $primaryKey = 'id';
protected $guarded = ['id'];
protected $table = 'wxtoken';
public $timestamps = true;
}数据库: CREATE TABLE `my_wxtoken` ( `id` int(11) NOT NULL AUTO_INCREMENT, `token` varchar(50) NOT NULL, `vx_name` varchar(50) DEFAULT NULL, `email` varchar(50) DEFAULT NULL, `wid` varchar(50) DEFAULT NULL, `is_check` tinyint(1) DEFAULT '0', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 insert into `my_wxtoken`(`id`,`token`,`vx_name`,`email`,`wid`,`is_check`,`created_at`,`updated_at`) values (1,'xiaoshu','xiaoshu_611','xiaoshu.611@163.com','gh_73ea9f484751',0,'2017-08-08 17:39:15','2017-08-08 17:39:15');
本文由:xiaoshu168 作者:xiaoshu发表,转载请注明来源!