2010-12-07 22 views
43

encontré esta función que hace un trabajo impresionante (en mi humilde opinión): http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curlPHP CURL y HTTPS

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

El único problema que tengo es que no funciona para https: //. ¿Qué ideas tengo que hacer para que esto funcione para https? ¡Gracias!

+4

definir "no funciona", por favor. –

+3

curl de forma predeterminada compruebe si el certificado SSL es válido ... es posible que desee deshabilitar ese comportamiento si firmó el certificado en cuestión – RageZ

+0

@RegeZ - ¿cómo hacer su sugerencia? – StackOverflowNewbie

Respuesta

70

solución rápida, añadir esto en sus opciones:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false) 

o simplemente añadirlo a su función actual:

/** 
* 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; 
} 
+2

Puede encontrar más información al respecto aquí: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to- access-https-ssltls-protected-sites/ – SystemX17

+0

disculpa por la edición incorrecta ;-) No entendí lo que querías decir ... – RageZ

+0

está bien, solo soy nuevo aquí desde ayer. gracias por hacer que el código sea mejor para la persona que pregunta, es increíble. – SystemX17

21

yo estaba tratando de usar curl para hacer algo de API https llama con php y se encontró con este problema. Me di cuenta de una recomendación en el sitio php que me puso en funcionamiento: 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!

+1

Lo hice, pero ahora solo recibo '' error 35: 14077410: rutinas SSL: SSL23_GET_SERVER_HELLO: error de handshake de alerta sslv3'' – OZZIE

0

otra opción como respuesta Gavin Palmer es utilizar el archivo .pem pero con una opción de rizo

1- descarga la última actualización .pem archivo desde https://curl.haxx.se/docs/caextract.html y guardarlo en algún lugar de su servidor (fuera de la carpeta pública)

2- establecer la opción en el código en lugar del archivo php.ini

curl_setopt($ch, CURLOPT_CAINFO, $_SERVER['DOCUMENT_ROOT'] . "/../cacert-2017-09-20.pem");