Curl https和http的GET和POST请求方法

IT-Pony 2016-03-20 AM 573℃ 0条
/**
 * @descript GET请求
 * @param  [string]  $url  请求地址
 * @return [json]     $data 返回数据
 */
function https_get($url)
{
    //初始化一个会话
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //超时时间
    curl_setopt($ch, CURLOPT_TIMEOUT, 500);
    //判断一下是不是https
    if(strpos($url,'https') == 0){
        //跳过证书验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    //发送到那个地址
    curl_setopt($ch, CURLOPT_URL, $url);
    //执行会话    
    $result = curl_exec($ch);
    //关闭会话
    curl_close($ch);

    return $result;
}

/**
 * @descript POST请求
 * @param  [string]    $url    请求地址
 * @param  [array]     $array  发送数据
 * @return [json]       $data   返回数据
 */
function https_post($url,$data = '')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //判断一下是不是https
    if(strpos($url,'https') == 0){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    //判断是否要传数据
    if (!empty($data)){
        // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, 0); 
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    
    return $result;
}
标签: none

非特殊说明,本博所有文章均为博主原创。

评论啦~