phpyk
9/13/2018 - 9:25 AM

Curl请求类(get,post)

<?php
/**
 * Created by PhpStorm.
 * User: kk
 * Date: 2017/4/23
 * Time: 下午2:22
 */

namespace App\Utils;


class HttpCurl
{

    /**
     * 通过 CURL post数据 调用接口
     * @param  [string]  $uri            [description]
     * @param  [array]  $postData       [description]
     * @param  [boolean]  $postJson     [description]
     * @param  integer $connecttimeout [description]
     * @param  integer $timeout        [description]
     * @return [json]                  [description]
     * @author hujiashan <[<email address>]>
     */
    public static function postCURL($uri, $postData, $postJson = false, $connecttimeout = 300, $timeout = 200)
    {
        if(empty($uri) || empty($postData)) return false;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_POST, true);
        if($postJson){
            $postData = json_encode($postData,JSON_UNESCAPED_UNICODE);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: '.strlen($postData))
            );
        }else{
            $postData = http_build_query($postData);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }


    /**
     * 通过 CURL get数据 调用接口
     * @param  [string]  $uri            [description]
     * @param  integer $connecttimeout [description]
     * @param  integer $timeout        [description]
     * @return [bool|json]                  [description]
     */
    public static function getCURL($uri, $connecttimeout = 300, $timeout = 200)
    {
        if(empty($uri)) return false;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }


}