Home >Backend Development >PHP Tutorial >Detailed analysis of curl simulation http request example

Detailed analysis of curl simulation http request example

little bottle
little bottleforward
2019-04-23 11:01:314566browse

The main content of this article is to use curl to simulate HTTP requests. It has certain reference value. Friends in need can take a look. I hope it can help you.

Introduction

cURL's official definition is: curl is a command line tool for transferring data with URL syntax, that is, uses URL Syntax rules for command line tools to transfer data.

PHP supports the libcurl library created by Daniel Stenberg, which can connect and communicate with various servers and use various protocols. The protocols currently supported by libcurl include http, https, ftp, gopher, telnet, dict, file, and ldap. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP upload (can also be completed through PHP's FTP extension), HTTP form-based upload, proxy, cookies, username and password authentication.

Concept

Using cURL in PHP

Illustration:

cURL simulates get request

/**
 * get方式发送curl请求
 * @param string $url    请求服务器地址
 * @param array $header  请求头数据
 * @param int $timeout   超时时间
 * @return mixed
 * @author itbsl
 */
function curl_get($url, $header=[], $timeout=30) {

    //初始化curl
    $curl = curl_init();

    //设置curl(请求的服务器地址)
    //参数1: curl资源
    //参数2: 配置项名称
    //参数3: 配置项的值
    curl_setopt($curl, CURLOPT_URL, $url);

    //跳过安全证书验证
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  // 跳过证书检查

    //设置获取的信息以文件流的形式返回,而不是直接输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);

    //发出请求
    $result = curl_exec($curl);

    //关闭curl资源
    curl_close($curl);

    return $result;
}

cURL simulates post request

/**
 * post方式发送curl请求
 * @param string $url   请求的服务器地址
 * @param array $data   要发送的数据
 * @param array $header 请求头数据
 * @param int $timeout  超时时间
 * @return mixed
 * @author itbsl<itbsl@foxmail.com>
 */
function curl_post($url, $data=[], $header=[], $timeout=30) {

    //初始化curl
    $curl = curl_init();

    //设置curl(请求的服务器地址)
    //参数1: curl资源
    //参数2: 配置项名称
    //参数3: 配置项的值
    curl_setopt($curl, CURLOPT_URL, $url);

    //跳过安全证书验证
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  // 跳过证书检查

    //设置获取的信息以文件流的形式返回,而不是直接输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

    //设置请求方式为post请求
    curl_setopt($curl, CURLOPT_POST, true);

    //设置post方式提交时携带的数据
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);

    //发出请求
    $result = curl_exec($curl);

    //关闭curl资源
    curl_close($curl);

    return $result;
}

Related tutorials: PHP video tutorial

The above is the detailed content of Detailed analysis of curl simulation http request example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete