laravel8使用GuzzleHttp\Client类发起各类请求示例

使用场景:
laravel8 中的GuzzleHttp\Client类,发起get/post请求,以各种方式参数,获取数据
示例代码:

namespace App\Services;

use GuzzleHttp\Client as HttpClient;
use Illuminate\Support\Facades\Log;

class httpService
{
    private $sRequestUrl;//图片提取文字
    private $oHttp;

    public function __construct()
    {
        $this->sRequestUrl = 'http://xxxx.xxx.xx';
        $this->oHttp = new HttpClient();
    }


    /**
     * post请求 正常传参
     * @return false|mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function requestPostArrayData()
    {
        $aRequestData = [
            'form_params' => [
                'name' => 'lily',
                'sex' => 'girl',
                'age' => 12
            ],
            'timeout' => 300
        ];
        $sResult = $this->oHttp->post($this->sRequestUrl, $aRequestData)->getBody()->getContents();
        if(!$sResult) {
            Log::info('error:' . $sResult);
            return false;
        }
        $aResult = json_decode($sResult, true);
        return $aResult;
    }

    /**
     * 下载远程大文件到本地服务器并保存
     * @param $iMsgId
     * @param $sFilePath
     * @return string
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    protected function downloadBigFile($iMsgId, $sFilePath)
    {
        set_time_lim

你可能感兴趣的:(php,laravel)