CurlHttpRequestJson.php 1.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/8/19
 * Time: 9:06
 */

namespace Meibuyu\Micro\Shopify\tools;

use Exception;

/**
 * Curl的json格式请求
 * Class HttpRequestJson
 * @package Meibuyu\Micro\Tools
 */
class CurlHttpRequestJson
{

    protected function prepareRequest($headers, $data = [])
    {
        $data = json_encode($data);
        if (!empty($data)) {
            $headers['Content-type'] = 'application/json';
            $headers['Content-Length'] = strlen($data);
        }
        return [$headers, $data];
    }

    /**
     * @param $url
     * @param array $headers
     * @return array
     * @throws Exception
     * @author zero
     */
    public function get($url, $headers = [])
    {
        return CurlRequest::get($url, $headers);
    }

    /**
     * post请求
     * @param $url
     * @param $data
     * @param array $headers
     * @return array
     * @throws Exception
     * @author zero
     */
    public function post($url, $data, $headers = [])
    {
        [$headers, $data] = self::prepareRequest($headers, $data);
        return CurlRequest::post($url, $data, $headers);
    }

    /**
     * put请求
     * @param $url
     * @param $data
     * @param array $headers
     * @return array
     * @throws Exception
     * @author zero
     */
    public function put($url, $data, $headers = [])
    {
        [$headers, $data] = self::prepareRequest($headers, $data);
        return CurlRequest::put($url, $data, $headers);
    }

    /**
     * delete请求
     * @param $url
     * @param array $headers
     * @return array
     * @throws Exception
     * @author zero
     */
    public function delete($url, $headers = [])
    {
        return CurlRequest::delete($url, $headers);
    }

}