PHP中使用Guzzle发送POST请求

2018-10-17 17:00 By "Powerless" 5505 0 1

在需要使用的项目中的composer.json文件添加依赖项

"require": {
    "guzzlehttp/guzzle": "~6.0"
}

安装完毕后记得检查

require 'vendor/autoload.php';

开始使用

/**
 * @param $url  请求地址
 * @param $form 提交参数
 *
 * @return mixed
 */
public function saveApiData($url,$form)
{
    $client = new Client([
        'headers'   =>[
            'Accept'        => 'application/json',
            'Content-Type'  => 'application/json'
        ]
    ]);
    //请求类型包括get,delete,head,options,patch,post,put 这里用post示范
    $res = $client->post( $url, [ 'json' => $form ]);
    $data['code'] = $res->getStatusCode();
    $data['body'] = $res->getBody()->getContents();
    return $data;
}


评 论

Others Discussion

  • 一些常见的基础概念
    Posted on 2018-11-28 19:10
  • HTTP头中隐藏PHP版本号
    Posted on 2021-01-11 16:38
  • MySQL中的行级锁,表级锁,页级锁
    Posted on 2018-08-25 11:00
  • QPS、TPS、RT、吞吐量到底是什么
    Posted on 2020-02-02 01:15
  • MySQL事务介绍
    Posted on 2019-06-05 18:14
  • 能创建多少个 TCP 连接?
    Posted on 2021-08-02 16:00
  • PHP 基金会来啦!
    Posted on 2022-10-08 17:40
  • 2018年云计算热词
    Posted on 2019-06-12 18:19