PHP的HTTP客户端Guzzle
本文实例讲述了PHP的HTTP客户端Guzzle简单使用方法。分享给大家供大家参考,具体如下:
首先来一段官方文档对Guzzle的介绍:
然后cd到网站根目录,执行Composer命令下载Guzzle:(Linux环境)
composer require guzzlehttp/guzzle
下载完成后会生成一个vender文件夹:
在vender同级目录新建了一个guzzle.php来写例子。
【GET请求】
require './vendor/autoload.php';
//实例化客户端
$client = new GuzzleHttp\Client();
//构造url
$url = 'https://www.yzsite.cn';
//get请求
$res = $client->request('GET', $url);
//返回状态码
echo $res->getStatusCode();
//连贯操作
//$res = $client->request('GET', $url)->getBody()->getContents();
【POST请求】
require './vendor/autoload.php';
//实例化客户端
$client = new GuzzleHttp\Client();
//构造url
$url = 'https://www.yzsite.cn';
//post请求
$res = $client->request('POST', $url, [
'form_params' = [
'name'= 'lws',
'sex'= 'nan'
]
]);
//返回状态码
echo $res->getStatusCode();
//连贯操作
//$res->getBody()->getContents();
【POST文件上传】
require './vendor/autoload.php';
//实例化客户端
$client = new GuzzleHttp\Client();
//构造url
$url = 'https://www.yzsite.cn';
//post请求
$res = $client->request('POST', $url, [
'multipart' = [
[
'name'= 'name',
'contents'= 'lws'
],
[
'name'= 'sex',
'contents'= 'nan'
],
[
'name'= 'tupian',
'contents'= file_get_contents('1.jpg'),
'filename'= 'lws.jpg'
]
]
]);
//返回状态码
echo $res->getStatusCode();
【设置代理IP】
require './vendor/autoload.php';
//实例化客户端
$client = new GuzzleHttp\Client();
//构造url
$url = 'https://www.yzsite.cn';
//设置代理请求
$res = $client->request('GET', $url, [
'proxy' = '111.22.33.44:6666'
]);
//返回状态码
echo $res->getStatusCode();
【模拟请求头】
require './vendor/autoload.php';
//实例化客户端
$client = new GuzzleHttp\Client();
//构造url
$url = 'https://www.yzsite.cn';
$header = [
'Content-Type' => 'application/json',
'appKey'=> $app_key,
'timestamp' => $timestamp,
'sign' => $sign
];
$data = [
'orderNo' => $ccic_order['third_order_no'],
'voucherNo' => $ccic_order['voucher_no'],
'userNo' => $ccic_order['ecif_id'],
'productCode' => $ccic_order['service_id'],
'orderType' => 1,
'orderStatus' => 3, // 0:未使用,自费券购买后默认状态 1:使用中 2:使用完成 3:取消
'ownExpense' => 1,
'callbackType' => 2,
];
-----------------------------------
guzzle json 请求
$response = $guzzle->post($url, [
'headers' => $header,
'json' => $data
]);
$res = json_decode($response->getBody()->getContents(),true);
【记录Cookie】
require './vendor/autoload.php';
//实例化客户端
$client = new GuzzleHttp\Client(['cookie'= true]);
//构造url
$url = 'https://www.yzsite.cn/]/';
//设置代理请求
$res = $client->request('GET', $url);
//返回状态码
echo $res->getStatusCode();
【xml】
$client = new \GuzzleHttp\Client();
// 请求xml数据
$xmlRequest = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getChineseFonts xmlns="http://xxx.cn/">
<byFontsLength&lg12</byFontsLength>
</getChineseFonts>
</soap:Body>
</soap:Envelope>';
try {
$response = $client->post(
'http://www.xxx.cn/WebServices/RandomFontsWebService.asmx',
[
'body' => $xmlRequest,
'headers' => [
'Content-Type' => 'text/xml',
]
]
);
if ($response->getStatusCode() === 200) {
$xmlObj = simplexml_load_string($response->getBody()->getContents());
$xmlObj->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xmlResult = $xmlObj->xpath("soap:Body");
$result = (array)$xmlResult[0]->getChineseFontsResponse->getChineseFontsResult;
var_dump($result);
} else {
echo 'Response Failure !!!';
}
} catch (\Throwable $e) {
echo $e->getMessage();
}
//返回数据参考
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getChineseFontsResponse xmlns="http://xxx.cn/">
<getChineseFontsResult>
<string>职</string>
</getChineseFontsResult>
</getChineseFontsResponse>
</soap:Body>
</soap:Envelope>

