Skip to content

Curl Http Client Builder, Support GET, POST, PUT, DELETE ,HEADER, TRACE Method

License

Notifications You must be signed in to change notification settings

yeahhub/httpbuilder

 
 

Repository files navigation

Curl Http Client

Software license Latest tag Build status Code_size

Usage

Http Client Support Http Method : GET, POST, PUT , DELETE , HEADER , TRACE

构建 HttpClient

protected $client ;
function __construct()
{
    $this->client = HttpClientBuilder::create()->build(); 
    // 如果你想使用自带的日志类,如 laravel中 Log
    $this->client = HttpClientBuilder::create()->build(Log::class); 

}

GET Request

$data = [
    'uri'=>'https://www.baidu.com',
    'headers'=>[
        'Content-Type'=>'application/json',
        'X-HTTP-Method-Override':'GET'
    ]
];

return $this->client->Get($data);

POST Request

$data = [
    'uri'=>'https://www.baidu.com',
    'headers'=>[
        'Content-Type'=>'application/json',
        'X-HTTP-Method-Override':'POST'
    ],
    'params'=> [
        'user'=>"username:ethan"
     ]
];

return $this->client->Post($data);

PUT 、DELETE Request

$data = [
    'uri'=>'https://www.baidu.com',
    'headers'=>[
        'Content-Type'=>'application/json',
        'X-HTTP-Method-Override':'PUT'
    ],
    'params'=> [
        'user'=>"username:ethan"
     ]
];

return $this->client->Put($data); // Put,Delete($data)

HEADER 、TRACE Request

$data = [
    'uri'=>'https://www.baidu.com',
    'headers'=>[
        'Content-Type'=>'application/json',
        'X-HTTP-Method-Override':'HEADER'
    ],
    'params'=> [
        'user'=>"username:ethan"
     ]
];

return $this->client->Header($data); // Header,Trace($data)

扩展

文件上传

$file = Request::file('temp'); // 接收前端传递的文件data
$originalName = $file->getClientOriginalName(); // 文件原名
$ext = $file->getClientOriginalExtension();     // 扩展名
$realPath = $file->getRealPath();   //临时文件的绝对路径
$type = $file->getClientMimeType();
$file_obj = new \CURLFile($realPath, $type, $originalName);
$data = [
            'url'=> $url,
            'params'=>[
                'file'=>$file_obj
            ]
        ];

return $this->client->Post($data); // Upload($data)

About

Curl Http Client Builder, Support GET, POST, PUT, DELETE ,HEADER, TRACE Method

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%