PHP code example of du-yong / http-client
1. Go to this page and download the library: Download du-yong/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/ */
du-yong / http-client example snippets
use httpClient\HttpClient;
//简单使用
//get
echo HttpClient::get("https://www.baidu.com/")->getBody();
//post
$response = HttpClient::post("http://qzshop.93dd.top/test",['code'=>1,'data'=>'test']);
print_r($response->getBody());
//post请求结果解析为json/array
print_r($response->parseJson->getBody());
print_r($response->parseArray->getBody());
//post json数据
HttpClient::postJson("http://www.baidu.com",['code'=>1,'data'=>'test']);
//获取错误结果
print_r($response->getError());
print_r($response->getErrno());
//获取响应的请求头
print_r($response->getHeader());
//使用代理
HttpClient::instance()->setProxy('ip:port')->send('https:://www.baidu.com');
//上传文件
HttpClient::postFile("http://www.baidu.com",['code'=>1,'file'=>'/usr/local/test.png']);//可以使用相对路径
//使用session
$session = HttpClient::session();
$response = $session::post("http://www.baidu.com",[
'username'=>'username',
'password'=>'password'
]);
echo "header: ";
print_r($response->getHeader());
echo "body: ";
print_r($response->getBody());
//get
HttpClient::instance()->send('https:://www.baidu.com');
//post
HttpClient::instance()->setPostData(array())->send('url','POST);
//post请求结果解析为json/array
HttpClient::instance()->setPostData(array())->send('url','POST)->parseJson()->getBody();
HttpClient::instance()->setPostData(array())->send('url','POST)->parseArray()->getBody();
//post json数据
HttpClient::instance()->setPostData(array(),true)->send('url','POST);
//获取错误结果
HttpClient::instance()->send('https:://www.baidu.com')->getError();
//获取响应的请求头
HttpClient::instance()->send('https:://www.baidu.com')->getHeader();
//使用代理
HttpClient::instance()->setProxy('ip:port')->send('https:://www.baidu.com');
//上传文件
HttpClient::instance()->setPostData(array('file'=>new \CURLFile($path)))->setUpload()->send('url','POST);PHP >5.5
HttpClient::instance()->setPostData(array('file'=>'@'.$path))->setUpload()->send('url','POST); PHP < 5.5