Home > Backend Development > PHP Tutorial > How Can I Modify My PHP `get_web_page` Function to Handle HTTPS Requests Using cURL?

How Can I Modify My PHP `get_web_page` Function to Handle HTTPS Requests Using cURL?

Susan Sarandon
Release: 2024-12-16 18:53:10
Original
171 people have browsed it

How Can I Modify My PHP `get_web_page` Function to Handle HTTPS Requests Using cURL?

PHP CURL & HTTPS

This article aims to address the issue of implementing the get_web_page function for HTTPS requests in PHP using CURL.

The provided function flawlessly handles HTTP requests. However, when dealing with HTTPS connections, it encounters difficulties. To resolve this, we'll incorporate an essential modification into the function.

By adding the following line to the options array:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false)
Copy after login

we instruct CURL to disable SSL certificate verification. This is a quick solution, but it also increases your vulnerability to man-in-the-middle attacks.

Alternatively, you can include this modification directly into the function:

/**
 * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
 * array containing the HTTP server response header fields and content.
 */
function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        CURLOPT_SSL_VERIFYPEER => false     // Disabled SSL Cert checks
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}
Copy after login

With this modification, the get_web_page function will effectively handle both HTTP and HTTPS requests.

The above is the detailed content of How Can I Modify My PHP `get_web_page` Function to Handle HTTPS Requests Using cURL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template