function request_url($url, $post = array(), $is_async = FALSE, $retry = 0, $verify_ssl = false)
{
    if (empty($url)) return '';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    // 需要以post的方式发送的数据
    if (!empty($post)) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($post) ? http_build_query($post): $post);
    }

    // HTTPS
    if (!$verify_ssl) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
    }

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); // 自动设置Referer
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回curl获取到的内容而不是直接输出
    curl_setopt($ch, CURLOPT_HEADER, false); // 不显示返回的header内容
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 5秒超时
    $content = curl_exec($ch);

    if($content === false){
        Log::info('Http request message :'.curl_error($ch));
    }

    // 重试
    if ($retry > 0 && $content === false) {
        $try = 0;
        do {
            $content = curl_exec($ch); ++$try;
        }
        while ($content === false && $try <= $retry);
    }

    /*$content === false && Log::error('helper request_url', array(
        'message' => curl_error($ch),
        'line' => curl_errno($ch),
        'url' => $url,
        'data' => $post,
    ));*/

    // 处理BOM
    /*if (preg_match('/^\xEF\xBB\xBF/', $content)) {
        $content = substr($content, 3);
    }*/

    curl_close($ch);
    return $content;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注