lablnet/http-client

PHP http 客户端包。

1.1 2020-02-15 19:21 UTC

This package is auto-updated.

Last update: 2024-09-23 19:41:28 UTC


README

http-client 是一个 PHP 包。它提供管理和解析请求对象的能力,同时也支持通过 cURL 进行 HTTP 客户端事务。

需求

  • PHP
  • Composer

安装

运行以下命令 composer require lablnet/http-client

基本用法

请求对象,GET 示例

让我们使用一个 GET 请求,URL 为 '/index.php?var=value'

<?php 
require '../vendor/autoload.php';

$request = new Lablnet\Request();

// Get the value of _GET['var']
$var = $request->getQuery('var');
//it print the get request	
var_dump($var);

请求对象,POST 示例

让我们使用一个 POST 请求。

// Get the value of $_POST['id']
if ($request->isPost()) {
	$id = $request->getPost('id');
}

创建响应对象

//Response
$config = [
	'code'    => 200,
	'headers' => [
		'Content-Type' => 'text/html'
	]
];

$response = new Lablnet\Response($config);
$response->setBody('This is a plain text file.');

$response->send();

简单的响应重定向

//Redirect to other page/site.
(new Lablnet\Redirect('https://zestframework.xyz/'));

使用 cURL 客户端

//Using the cURL client	
//Send request to https://zestframework.xyz login page with post method
 $request = $request->curl("https://zestframework.xyz/account/login/action","POST");
 //Set transfer and return header
$request->setReturnHeader(true)->setReturnTransfer(true);
//Set the fields
$request->setFields([
	'username'  => 'your-username',
	'password' => 'your-password'
]);
//Send the request
$request->send();
// return => 200
$statusCode = $request->getCode();
// Display the body of the returned response
echo "<br\>".$request->getBody();