Home >Backend Development >PHP Tutorial >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)
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; }
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!