安裝所需要的包
composer require guzzlehttp/guzzle
composer require illuminate/http
文件,重點關注 Request 和 Response
使用示例
<?php
use Illuminate\Http\Client\Factory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
require_once __DIR__ . '/../vendor/autoload.php';
date_default_timezone_set('UTC');
$http = new Factory();
//$response = $http->post('https://jsonplaceholder.typicode.com/posts', [
// 'title' => 'foo',
// 'body' => 'bar',
// 'userId' => 1,
//]);
//$id = $response->json()['id'];
//
//$response = $http->get('https://jsonplaceholder.typicode.com/posts');
//echo $response->body();
// http://localhost:8000/index/http.php?name=Chrome&type=1&from=browser
// 需要專案入口檔案是 index.php 且請求路徑隱藏 例如:http://localhost:8000?name=Chrome&type=1&from=browser
$request = new Request();
$name = $request->input('name', 'Joey');
mm($name);
mm($request->has('name'));
mm($request->all());
mm($request->path());
mm($request->url());
mm($request->fullUrl());
mm($request->method());
mm($request->isMethod('get'));
$response = new Response();
$response->setContent(['name' => $name])->setStatusCode(200)->send();
命令列開啟服務 php -S localhost:8000
並訪問 http://localhost:8000/index/http.php
即可。
todo
封裝 api 返回
參考 致謝
本作品採用《CC 協議》,轉載必須註明作者和本文連結