首先安装composer
移步:https://getcomposer.org/download/

切换成国内镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com
使用composer下载guzzle到项目中
切换目录到thinkphp项目的vendor目录中然后composer require guzzlehttp/guzzle

使用
首先引用
include 'vendor/guzzle/autoload.php';
use GuzzleHttp\Client;
然后方法里面就可以使用了(如果报curl: (60) SSL certificate
错误,在下方有解决方法)
$client = new Client();
$url = "http://www.baidu.com";
$response = $client->request('GET', $url);
// echo $response->getReasonPhrase();//获取相应结果
// echo $response->getStatusCode();//获取状态码
// echo $response->getHeaders();//获取响应头信息
// echo $response->getHeader('Content-Typt')[0];//获取响应头的某一项
$body = $response->getBody();//获取相应体
$html = $body->getContents();//获取目标页面
echo $html;
然后就可以看到熟悉的百度首页

使用过程中的小问题
- 报错:
curl: (60) SSL certificate : unable to get local issuer certificate
解决:点击http://curl.haxx.se/ca/cacert.pem 下载证书,放到你的php安装目录里面(php.exe的那个文件夹)然后打开php.ini
搜索curl.cainfo
,把目录添加上,重启php即可.
image.png
网友评论