PHP的HTTP客户端Guzzle简单使用方法分析

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP的HTTP客户端Guzzle简单使用方法。分享给大家供大家参考,具体如下:

首先来一段官方文档对Guzzle的介绍:

然后cd到网站根目录,执行Composer命令下载Guzzle:(Linux环境)

composer require guzzlehttp/guzzle

下载完成后会生成一个vender文件夹:

在vender同级目录新建了一个guzzle.php来写例子。

【GET请求】

<?php 
  require './vendor/autoload.php';
 
  //实例化客户端
  $client = new GuzzleHttp\Client();
 
  //构造url
  $url = 'https://www.baidu.com';
 
  //get请求
  $res = $client->request('GET', $url);
 
  //返回状态码
  echo $res->getStatusCode();
 
  //连贯操作
  //$res = $client->request('GET', $url)->getBody()->getContents();
 ?>

【POST请求】

<?php 
  require './vendor/autoload.php';
 
  //实例化客户端
  $client = new GuzzleHttp\Client(); 
 
  //构造url
  $url = 'https://www.baidu.com';
 
  //post请求
  $res = $client->request('POST', $url, [
    'form_params' => [
      'name'=>'lws',
      'sex'=>'nan'
    ]
  ]);
 
  //返回状态码
  echo $res->getStatusCode();
?>

【POST文件上传】

<?php 
  require './vendor/autoload.php';
 
  //实例化客户端
  $client = new GuzzleHttp\Client(); 
 
  //构造url
  $url = 'https://www.baidu.com';
 
  //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】

<?php 
  require './vendor/autoload.php';
 
  //实例化客户端
  $client = new GuzzleHttp\Client(); 
 
  //构造url
  $url = 'https://www.baidu.com';
 
  //设置代理请求
  $res = $client->request('GET', $url, [
    'proxy' => '111.22.33.44:6666'
  ]);
 
  //返回状态码
  echo $res->getStatusCode();
?>

【模拟请求头】

<?php 
  require './vendor/autoload.php';
 
  //实例化客户端
  $client = new GuzzleHttp\Client(['headers'=>['referer'=>'https://www.baidu,com']]); 
 
  //构造url
  $url = 'https://www.baidu.com';
 
  //设置代理请求
  $res = $client->request('GET', $url);
 
  //返回状态码
  echo $res->getStatusCode();
?>

【记录Cookie】

<?php 
  require './vendor/autoload.php';
 
  //实例化客户端
  $client = new GuzzleHttp\Client(['cookie'=>true]); 
 
  //构造url
  $url = 'https://www.baidu.com';
 
  //设置代理请求
  $res = $client->request('GET', $url);
 
  //返回状态码
  echo $res->getStatusCode();
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php socket用法总结》、《php字符串(string)用法总结》、《PHP数学运算技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《PHP网络编程技巧总结

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP常量define和const的区别详解

前言 常量是一个简单的标识符。在脚本执行期间该值不能改变(除了所谓的魔术常量,他们其实不是常量)。常量默认大小写敏感。通常常量标识符总是大写的。 可以用define()函数来定义常量。...

PHP面向对象程序设计__tostring()和__invoke()用法分析

本文实例讲述了PHP面向对象程序设计__tostring()和__invoke()用法。分享给大家供大家参考,具体如下: __tostring()魔术方法 将一个对象当做一个字符串来使用...

PHP对象的浅复制与深复制的实例详解

PHP对象的浅复制与深复制的实例详解 最近在看原型模式时注意到这个问题~~PHP中对象 '=' 与‘clone'的区别 实例代码: //聚合类 class ObjA { p...

php简单实现发送带附件的邮件

本文实例讲述了php简单实现发送带附件的邮件。分享给大家供大家参考。具体如下: 下面是静态html代码: <html> <head> <title&...

thinkphp多表查询两表有重复相同字段的完美解决方法

框架:thinkphp 版本:3.2.3 内容:查询语句 解决问题:重复字段问题 $Data = M('a')->where($where) ->Fiel...