PHP code example of hollisho / http-client
1. Go to this page and download the library: Download hollisho/http-client 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/ */
hollisho / http-client example snippets
if (file_exists(dirname(__FILE__) . '/vendor/autoload.php')) {
_VERSION_ID >= 80000) {
// PHP 8.0+ 需要明确注册注解命名空间
if (class_exists('Doctrine\Common\Annotations\AnnotationRegistry')) {
// 注册类加载器
AnnotationRegistry::registerLoader('class_exists');
// 显式注册 hollisho 的注解命名空间
if (method_exists(AnnotationRegistry::class, 'registerAutoloadNamespace')) {
AnnotationRegistry::registerAutoloadNamespace(
'hollisho\httpclient\Annotations',
dirname(__FILE__) . '/vendor/hollisho/http-client/src/Annotations'
);
}
}
} else {
// PHP 7.x
if (method_exists(AnnotationRegistry::class, 'registerLoader')) {
AnnotationRegistry::registerLoader('class_exists');
}
}
$httpClient = new BaseClient('https://www.1024plus.com');
$httpClient->pushMiddleware(new AuthRequest());
$httpClient->httpPost('/category/springboot/');
/**
* @author Hollis
* Interface UserService
*
* @BaseUrl(host="https://www.1024plus.com/")
*
* @package hollisho\httpclientTests\Service
*/
interface UserService
{
/**
* @Headers(headers={
* @AuthBasic(username="override", password="override"),
* @CustomHeader(name="x-override", body="test")
* })
*
* @Action(
* method=@Get,
* endpoint=@Endpoint(uri="/api/entry/{id}")
* )
*/
public function getUser($id);
/**
* @Action(
* method=@Post,
* endpoint=@Endpoint(uri="/resource"),
* body=@Body(json=true, name="body")
* )
*/
public function createUser($data);
}
// 生成 FeignClient 实例
$client = FeignClientFactory::create(UserService::class);
echo $client->getUser(1);
sh
$ ./vendor/phpunit/phpunit/phpunit --configuration phpunit.xml --test-suffix RequestTest.php