PHP中使用Guzzle发送POST请求

2018-10-17 17:00 By "Powerless" 5507 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;
}


评 论

View in WeChat

Others Discussion

  • 分布式架构之「 数据分布」
    Posted on 2019-11-14 10:00
  • PHP扩展ImageMagick安装
    Posted on 2022-11-11 11:16
  • PHP7不兼容性
    Posted on 2018-03-07 15:59
  • Redis各种数据类型的使用场景举例分析【三】
    Posted on 2018-11-22 17:00
  • 投票通过,PHP 8 确认引入 Union Types 2.0
    Posted on 2019-11-18 22:22
  • PHP设计模式 - 委托模式
    Posted on 2019-04-25 16:15
  • BASE原则
    Posted on 2020-12-17 16:42
  • 程序员年中考试题-段子版
    Posted on 2021-06-23 15:57