美文网首页
PHP CURL请求

PHP CURL请求

作者: 阡洛 | 来源:发表于2020-06-15 14:12 被阅读0次
/**
 * 发送HTTP请求方法,目前只支持CURL发送请求
 * @param  string $url     请求URL
 * @param  string $type    请求方法GET/POST
 * @param  array  $data    请求参数
 * @param  array  $header  请求header 
 * @param string $timeout 超时时间
 */
function request($url, $type, $data = false, $header = [], $timeout = 30)
{
    $cl = curl_init();
    //兼容HTTPS
    if (stripos($url, "https://") !== FALSE)
    {
        curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($cl, CURLOPT_SSLVERSION, 1);
    }

    //设置返回内容做变量存储
    curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
    //设置需要返回Header
    curl_setopt($cl, CURLOPT_HEADER, true);
    //设置请求头
    if (count($header) > 0)
    {
        $header = array_merge($header, array("Expect:", "Connection:Keep-Alive", "Keep-Alive:300"));
        curl_setopt($cl, CURLOPT_HTTPHEADER, $header);
    }
    else
    {
        curl_setopt($cl, CURLOPT_HTTPHEADER, array("Expect:", "Connection:Keep-Alive", "Keep-Alive:300"));
    }
    //设置需要返回Body
    curl_setopt($cl, CURLOPT_NOBODY, 0);
    //设置超时时间
    if ($timeout > 0)
    {
        curl_setopt($cl, CURLOPT_TIMEOUT, $timeout);
    }

    curl_setopt($cl, CURLOPT_FOLLOWLOCATION, true); 
    // POST/GET参数处理
    if (strtoupper($type) == "POST")
    {
        curl_setopt($cl, CURLOPT_POST, true);
        if (class_exists("\CURLFile") && is_array($data))
        {
            foreach ($data as $k => $v)
            {
                if (is_string($v) && strpos($v, "@") === 0)
                {
                    $v = ltrim($v, "@");
                    $data[$k] = new \CURLFile($v);
                }
            }
        }
        curl_setopt($cl, CURLOPT_POSTFIELDS, $data);
    }

    if ($type == "GET" && is_array($data))
    {
        if (stripos($url, "?") === FALSE)
        {
            $url .= "?";
        }
        $url .= http_build_query($data);
    }

    curl_setopt($cl, CURLOPT_FORBID_REUSE, 0);
    curl_setopt($cl, CURLOPT_URL, $url);
    //curl_setopt($cl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'); //指明以哪种方式进行访问
    //读取获取内容
    $response = curl_exec($cl);

    //读取状态
    $status = curl_getinfo($cl);
    //读取错误号
    $errno = curl_errno($cl);
    //读取错误详情
    $error = curl_error($cl);
    //关闭Curl
    curl_close($cl);

    if ($errno == 0 && isset($status["http_code"]))
    {
        $header = substr($response, 0, $status["header_size"]);
        $body = substr($response, $status["header_size"]);
        return array($body, "");
    }
    else
    {
        return array("", $error);
    }
}

Expect

在不设置 Expect 头信息使用 curl 发送 POST 请求时,如果 POST 数据大于 1kb,curl 默认行为如下:

  1. 先追加一个Expect: 100-continue请求头信息,发送这个不包含 POST 数据的请求;
  2. 如果服务器返回的响应头信息中包含Expect: 100-continue,则表示 Server 愿意接受数据,这时才 POST 真正数据给 Server。

Failed to connect to 10.0.0.0 port 9000: Adress already in use

在头信息中添加Connection:Keep-Alive,Keep-Alive:300
Keep-Alive选项用于限制保持多长时间。

相关文章

网友评论

      本文标题:PHP CURL请求

      本文链接:https://www.haomeiwen.com/subject/hgurahtx.html