php guzzle 上传图片,PHP中使用Guzzle发送POST请求

在需要使用的项目中的composer.json文件添加依赖项 "require": { "guzzlehttp/guzzle": "~6.0" } 安装完毕后

在需要使用的项目中的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;

}