curl impersonates http requests

brief introduction

curl is a command line tool for transferring data with URL syntax.

PHP supports libcurl library created by Daniel Stenberg, which can connect to various servers and use various protocols. Currently, libcurl supports http, HTTPS, ftp, gopher, telnet, dict, file and ldap. Libcurl also supports HTTPS certificate, HTTP POST, HTTP PUT, ftp upload (which can also be completed through PHP's ftp extension), http form based upload, proxy, cookies, user name + password authentication.

concept

Using cURL in PHP

The picture shows:

cURL simulate get request

/**
 * get Send curl request by
 * @param string $url    Request server address
 * @param array $header  Request header data
 * @param int $timeout   Timeout time
 * @return mixed
 * @author itbsl
 */
function curl_get($url, $header=[], $timeout=30) {

    //Initialize curl
    $curl = curl_init();

    //Set curl (requested server address)
    //Parameter 1: curl resource
    //Parameter 2: configuration item name
    //Parameter 3: value of configuration item
    curl_setopt($curl, CURLOPT_URL, $url);

    //Skip security certificate validation
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // Check if SSL encryption algorithm exists from certificate
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  // Skip certificate check

    //Set the information obtained to be returned as a file stream instead of being output directly
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);

    //Request
    $result = curl_exec($curl);

    //Close curl resource
    curl_close($curl);

    return $result;
}

cURL impersonate post request

/**
 * post Send curl request by
 * @param string $url   Requested server address
 * @param array $data   Data to send
 * @param array $header Request header data
 * @param int $timeout  Timeout time
 * @return mixed
 * @author itbsl<itbsl@foxmail.com>
 */
function curl_post($url, $data=[], $header=[], $timeout=30) {

    //Initialize curl
    $curl = curl_init();

    //Set curl (requested server address)
    //Parameter 1: curl resource
    //Parameter 2: configuration item name
    //Parameter 3: value of configuration item
    curl_setopt($curl, CURLOPT_URL, $url);

    //Skip security certificate validation
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // Check if SSL encryption algorithm exists from certificate
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  // Skip certificate check

    //Set the information obtained to be returned as a file stream instead of being output directly
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

    //Set request mode to post request
    curl_setopt($curl, CURLOPT_POST, true);

    //Set the data carried in post mode submission
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);

    //Request
    $result = curl_exec($curl);

    //Close curl resource
    curl_close($curl);

    return $result;
}

Tags: PHP curl ftp SSL

Posted on Mon, 09 Dec 2019 15:20:19 -0800 by boushley