php使用curl发起请求示例

php使用curl发起各种请求示例代码如下:

/**
     * 与接口通信工具
     * @param string $sUrl
     * @param array $aData
     * @param string $sMethod
     * @param int $iTimeout
     * @param array $aHeader
     * @param int $iJsonDecode
     * @return array
     */
    public static function requestApi($sUrl, $aData, $sMethod = 'Post', $iTimeout = 10, $aHeader = [], $iJsonDecode = 1)
    {
        $sMethod = strtolower($sMethod);
        $ch = curl_init();
        if ($sMethod == 'get') {
            $sUrl .= '?' . http_build_query($aData);
        }
        if (!empty($aHeader)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
        curl_setopt($ch, CURLOPT_URL, $sUrl);
        if ($sMethod == 'post') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $aData);
        }
        $iTimeout = intval($iTimeout);
        if ($iTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $iTimeout);
        }
        ob_start();
        curl_exec($ch);
        $sOut = ob_get_clean();
        curl_close($ch);
        if ($iJsonDecode) {
            return json_decode($sOut, true);
        }
        return $sOut;
    }

    public static function httpGet($url, $data = '')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        if ($data) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        $res = curl_exec($curl);
        curl_close($curl);

        return json_decode($res, true);
    }

	//post请求
    public static function httpsPost($sUrl, $sBody = '')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $sUrl);
        if (!empty($sBody)) {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $sBody);
        }
        $res = curl_exec($curl);
        curl_close($curl);

        return json_decode($res, true);
    }

   /**
     * @param string $sUrl 请求API链接
     * @param array $aData 发送的内容
     * @param string $sMethod 发送请求的方式
     * @param int $iTimeout
     */
    public static function sendApiRequest($sUrl, $aData = [], $sMethod = 'post', $iTimeout = 10, $aHeaders = [], $sType = "form_params")
    {
        $res = false;
        $client = new \GuzzleHttp\Client([
            'connect_timeout' => 3,  // 连接超时三秒钟,断开
            'read_timeout' => $iTimeout,  // 读取内容超过10秒钟,视为超时
            'verify' => false,  // 不检查ssl
            'timeout' => $iTimeout,  // 发送过程超过10秒钟算作超时
        ]);

        try {
            if (strtolower($sMethod) == 'get') {
                if (count($aData)) {
                    $options = ['query' => $aData];
                } else {
                    $options = [];
                }
                $res = $client->request('get', $sUrl, $options)->getBody()->getContents();
            } else if (strtolower($sMethod) == 'post') {
                $res = $client->request('post', $sUrl, [
                    'headers' => $aHeaders,
                    $sType => $aData
                ])->getBody()->getContents();
            }
        } catch (Exception  $exception) {
            info($exception);
        }
        return $res;
    }

    //post请求 http body传json数据
    public static function httpBodyPost($sUrl, $sJsonStr, $iTime = 10)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $sUrl);
        curl_setopt($ch, CURLOPT_TIMEOUT, $iTime);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $sJsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //关闭ssl验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($sJsonStr)
            ]
        );
        $response = curl_exec($ch);
        curl_close($ch);

        return $response;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值