封裝 PHP curl http 請求 (全) Composer 安裝 httpbuilder,支援 GET,POST,PUT,DELETE

Ethansmart發表於2018-10-06
composer 安裝:composer require ethansmart/httpbuilder
github 地址:https://github.com/roancsu/httpbuilder

在PHP 框架中可以使用 guzzlehttp 來構建HTTP Request 服務,但是guzzle 太重了,用起來比較繁瑣,所以我用curl封裝了PHP HTTP Request Client,支援 GET,POST,PUT,DELETE 方法,並且對檔案上傳有安全檢測功能等等,使用也非常簡單,效率高。

Usage:

構建 HttpClient

protected $client ;
function __construct()
{
    $this->client = HttpClientBuilder::create()->build();
}

GET Request

$data = [
    'uri'=>'https://www.baidu.com'
];

return $this->client
    ->setHeaders('Content-Type:application/json')
    ->setHeaders('X-HTTP-Method-Override:GET')
    ->setHeaders('Request_id: Ethan')
    ->setTimeout(10)
    ->Get($data);

POST Request


$data = [
    'uri'=>'https://www.baidu.com',
    'params'=> [
        'user'=>ethan
     ]
];

return $this->client
    ->setHeaders('Content-Type:application/json')
    ->Post($data);

PUT 、DELETE Request


$data = [
    'uri'=>'https://www.baidu.com',
    'params'=> [
        'user'=>ethan
     ]
];

return $this->client
    ->setHeaders('Content-Type:application/json')
    ->Put($data); // Delete($data)

擴充套件
檔案上傳

Http/Client.php
但post 資料 大於1k時,設定Expect ;

<?php

namespace App\Services\Tools\HttpClient\Http;

use Log;

/**
 * Class HttpClient
 * @package Ethansmart\HttpBuilder\Http
 * Support Http Method : GET, POST, PUT , DELETE
 */

class Client
{
    private $ch ;
    private $url ;
    private $method ;
    private $params ;
    private $timeout;
    protected $multipart ;

    public function __construct()
    {
        $this->timeout = 120 ;
    }

    public function Get($data)
    {
        $data['method'] = "GET";
        return $this->performRequest($data);
    }

    public function Post($data)
    {
        $data['method'] = "POST";
        return $this->performRequest($data);
    }

    public function Put($data)
    {
        $data['method'] = "PUT";
        return $this->performRequest($data);
    }

    public function Delete($data)
    {
        $data['method'] = "DELETE";
        return $this->performRequest($data);
    }

    public function Upload($data)
    {
        $data['method'] = "POST";
        $this->multipart = true;
        return $this->performRequest($data);
    }

    /**
     * Http 請求
     * @param $data
     * @return array
     */
    public function performRequest($data)
    {
        $this->ch = curl_init();
        $url = $data['url'];
        try {
            $this->dataValication($data);
        } catch (\Exception $e) {
            return ['code'=>-1, 'msg'=>$e->getMessage()];
        }

        $timeout = isset($data['timeout'])?$data['timeout']:$this->timeout;
        $headers = $this->setHeaders($data);
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($this->ch, CURLOPT_HEADER, true);
        curl_setopt($this->ch, CURLINFO_HEADER_OUT, true);
        if (!empty($headers)) {
            curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
        }
        curl_setopt($this->ch, CURLOPT_NOBODY, false);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $this->method); //設定請求方式

        if ($this->method=="GET") {
            if(strpos($this->url,'?')){
                $this->url .= http_build_query($this->params);
            }else{
                $this->url .= '?' . http_build_query($this->params);
            }
        }else{
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->multipart?$this->params:http_build_query($this->params));
        }

        curl_setopt($this->ch, CURLOPT_URL, $this->url);

        if (1 == strpos('$'.$this->url, "https://")) {
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $result = curl_exec($this->ch);

//        if (curl_getinfo($this->ch, CURLINFO_HTTP_CODE) == '200') {}

        if(!curl_errno($this->ch)){
            list($response_header, $response_body) = explode("\r\n\r\n", $result, 2);
            Log::info("Request Headers: ". json_encode($response_header));
            Log::info("Request Body :".json_encode($response_body));
            $contentType = curl_getinfo($this->ch, CURLINFO_CONTENT_TYPE);

            $info = curl_getinfo($this->ch);
            Log::info('耗時 ' . $info['total_time'] . ' Seconds 傳送請求到 ' . $info['url']);
            $response = ['code'=>0, 'msg'=>'OK', 'data'=>$response_body, 'contentType'=>$contentType];
        }else{
            Log::info('Curl error: ' . curl_error($this->ch)) ;
            $response = ['data'=>(object)['code'=>-1, 'msg'=>"請求 $url 出錯: Curl error: ". curl_error($this->ch)]];
        }

        curl_close($this->ch);

        return $response;
    }

    /**
     * 設定Header資訊
     * @param $data
     * @return array
     */
    public function setHeaders($data)
    {
        $headers = array();
        if (isset($data['headers'])) {
            foreach ($data['headers'] as $key=>$item) {
                $headers[] = "$key:$item";
            }
        }
        $headers[] = "Expect:"; // post資料大於1k時,預設不需要新增Expect:100-continue

        return $headers;
    }

    public function setTimeout($timeout)
    {
        if (!empty($timeout) || $timeout != 30) {
            $this->timeout = $timeout ;
        }

        return $this;
    }

    /**
     * 資料驗證
     * @param $data
     * @throws \Exception
     */
    public function dataValication($data)
    {
        if(!isset($data['url']) || empty($data['url'])){
            throw new \Exception("HttpClient Error: Uri不能為空", 4422);
        }else{
            $this->url = $data['url'];
        }

        if(!isset($data['params']) || empty($data['params'])){
            $this->params = [];
        }else{
            $this->params = $data['params'];
        }

        if(!isset($data['method']) || empty($data['method'])){
            $this->method = "POST";
        }else{
            $this->method = $data['method'];
        }
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結
Ethan Smart & AI Algorithm

相關文章