ToolRepository.php 1.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2022/11/05
 * Time: 16:40
 */

namespace App\Repositories;


/**
 * Class ToolRepository
 * @package App\Repositories
 */
class ToolRepository
{
    /**
     * @param $url
     * @param $data
     * @param string $method
     * @param array $header
     * @param int $time_out
     * @return array
     */
    public function curlRequest($url, $data, $method = 'POST', $header = [], $time_out = 30)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if ($data)
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
                'Expect:',
                'Content-type: application/json',
                'Accept: application/json',
            ], $header)
        );
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        $response = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return [$code, $response];
    }
}