PHP code example of singiu / http-php
1. Go to this page and download the library: Download singiu/http-php 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/ */
singiu / http-php example snippets
use Singiu\Http\Http;
Http::setBaseUrl('http://localhost');
$response = Http::get('/api/user');
$response = Http::get('api/user', [ // 最开头的 "/" 不要也可以。
// 发送请求参数(当然你也可以自己拼接在 URL 的尾部):
'query' => [
'page' => 2
]
]);
$result = $response->getResponseObject();
echo $result->username;
use Singiu\Http\Http;
Http::setBaseUrl('http://localhost');
$response = Http:post('/api/user', [
// 发送数据,这里和 GET 方法有些不一样,因为考虑到有些请求需要 GET 和 POST 同时传参的情况。
'data' => [
'username' => 'singiu',
'phone' => 13838389438
]
]);
echo $response->getResponseText();
use Singiu\Http\Http;
$response = Http:get('/api/user', [
// timeout 设置请求连接等待超时时间,单位为秒;headers 可以设置请求头信息。
'timeout' => 30,
'headers' => [
'Content-Type' => 'application/json'
]
]);
$result = $response->getResponseArray();
$result['username'];
$response->getUrl(); // 获取该响应的请求 URL。
$response->getStatusCode(); // 获取该响应的 HTTP 响应码。
$response->getResponseText(); // 以文本格式获取响应的内容。
$response->getResponseArray(); // 如果返回的文本是正确的 JSON 格式,则会返回一个解析后的数组。
$response->getResponseObject(); // 如果返回的文本是正确的 JSON 格式,则会返回一个解析后的对象。