1. Go to this page and download the library: Download oh86/laravel-http-tools 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/ */
use Illuminate\Http\Request;
use Oh86\Http\Response\ErrorResponse;
use Oh86\Http\Response\OkResponse;
class XXXCtroller
{
public function ok(Request $request)
{
return new OkResponse();
}
public function error(Request $request)
{
return new ErrorResponse(1, null, [
'foo' => 'bar',
]);
}
}
use Oh86\Http\Exceptions\ErrorCodeException;
throw new throw new ErrorCodeException(401, null, null, 401); // 响应错误码为401的json响应。
use Illuminate\Support\Facades\Http;
use Oh86\Http\Exceptions\HttpRequestException;
$url = 'https://api.test/test';
$datas = ['foo' => 'bar'];
$headers = ['X-Test' => 'test'];
$r = Http::withHeaders($headers)->get($url, $datas);
if ($response->json('code') !== 0) {
// 将响应json响应给客户端
throw new HttpRequestException(
$r->status(),
$r->body(),
$url,
$datas,
$headers,
);
}
use Illuminate\Support\Facades\Route;
use Oh86\Http\Middleware\MutexRequestByArg;
Route::get('sms/code', [SmsController::class, 'sendSmsCode'])
->middleware(MutexRequestByArg::class.':phone');
use Oh86\Http\TokenSessions\AbstractTokenSession;
class TestToken extends AbstractTokenSession
{
protected $storeKey = 'TestToken';
protected $ttl = 300;
}
// 首次生成token
$t = new TestToken();
$t->put('foo', 'bar');
// $t->save(); // auto save in destructor
$token = $t->getToken();
// 其他地方使用token
$t = TestToken::load($token);
$val = $t->get('foo');
$t->destroy(); // 使用完手动销毁
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.