基本信息
- laravel5.4
- 西部數(shù)碼云服務(wù)器
- 微信開放平臺(tái)測(cè)試號(hào)
laravel開發(fā)重要的是路由,請(qǐng)求的大多數(shù)是POST和GET,在微信開發(fā)中需要注意的是:微信驗(yàn)證token的請(qǐng)求是GET方式,但是真正介入微信服務(wù)器采用的是POST方式,所以微信后臺(tái)配置驗(yàn)證接口時(shí)候,要使用GET方式,驗(yàn)證成功配置完成再改成POST方式。
路由:
Route::group(['prefix' => 'backed', 'namespace' => 'Backeds'], function () {
Route::get('weixin/token', 'WeixinController@token');
Route::post('weixin/token', 'WeixinController@token');
Route::any('weixin/api', 'WeixinController@api');//下面有具體方法代碼
});
laravel中還有一個(gè)需要注意的是中間件的概念,中間件是所有請(qǐng)求都需要經(jīng)過驗(yàn)證才能分發(fā)給相應(yīng)的控制器的玩意.laravel中POST提交都必須包含csrf防止跨域攻擊的,在微信開發(fā)接口中需要關(guān)閉:
打開:app\Http\Middleware\VerifyCsrfToken.php
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'weixin/api',
];
}
接下來在對(duì)應(yīng)的Controller中創(chuàng)建WeixinController.php
php artisan make:controller WeixinController
<?php
namespace App\Http\Controllers\Backeds;
use DB;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class WeixinController extends Controller
{
//驗(yàn)證消息
public function api()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
//檢查簽名
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = "weixin";//可以隨便填,只要和后臺(tái)添加的一樣就行
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
return true;
}else{
return false;
}
}
}
到此為止,打開微信公眾號(hào)后臺(tái),開始配置微信服務(wù)器
配置URL:http://wechat.xhqlzj.com/backed/weixin/api
配置Token:weixin(Token要和WeixinController中checkSignature方法里面定義的token一樣)

搞定
接下來會(huì)有詳細(xì)的微信開發(fā)文章,歡迎關(guān)注。獲取更多的相關(guān)資料可以關(guān)注公眾號(hào)

回復(fù)“資源”有驚喜