Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP CURL & HTTPS

Tags:

php

curl

https

I found this function that does an AWESOME job (IMHO): http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl

/**  * 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     );      $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; } 

The only problem I have is that it doesn't work for https://. Anny ideas what I need to do to make this work for https? Thanks!

like image 857
StackOverflowNewbie Avatar asked Dec 07 '10 01:12

StackOverflowNewbie


People also ask

What is a cURL in PHP?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.

Is cURL enabled in your PHP?

cURL is enabled by default but in case you have disabled it, follow the steps to enable it. Open php. ini (it's usually in /etc/ or in php folder on the server). Search for extension=php_curl.

What is cURL in PHP w3schools?

cURL is a library that lets you make HTTP requests in PHP. Everything you need to know about it (and most other extensions) can be found in the PHP manual. In order to use PHP's cURL functions you need to install the » libcurl package. PHP requires that you use libcurl 7.0.

What is the use of cURL ()?

cURL, which stands for client URL, is a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.


2 Answers

Quick fix, add this in your options:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false) 

Now you have no idea what host you're actually connecting to, because cURL will not verify the certificate in any way. Hope you enjoy man-in-the-middle attacks!

Or just add it to your current 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; } 
like image 75
SystemX17 Avatar answered Sep 23 '22 22:09

SystemX17


I was trying to use CURL to do some https API calls with php and ran into this problem. I noticed a recommendation on the php site which got me up and running: http://php.net/manual/en/function.curl-setopt.php#110457

Please everyone, stop setting CURLOPT_SSL_VERIFYPEER to false or 0. If your PHP installation doesn't have an up-to-date CA root certificate bundle, download the one at the curl website and save it on your server:

http://curl.haxx.se/docs/caextract.html

Then set a path to it in your php.ini file, e.g. on Windows:

curl.cainfo=c:\php\cacert.pem

Turning off CURLOPT_SSL_VERIFYPEER allows man in the middle (MITM) attacks, which you don't want!

like image 40
Gavin Palmer Avatar answered Sep 24 '22 22:09

Gavin Palmer