2010-12-07 11 views
43

J'ai trouvé cette fonction qui fait un excellent travail (IMHO): http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curlPHP CURL et 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; 
} 

Le seul problème que j'ai est que cela ne fonctionne pas pour https: //. Anny idées ce que je dois faire pour que cela fonctionne pour https? Merci!

+4

définir "ne fonctionne pas" s'il vous plaît. –

+3

curl par défaut vérifiez si le certificat SSL est valide ... vous pouvez désactiver ce comportement si vous vous êtes auto-signé le certificat en question – RageZ

+0

@RegeZ - comment faire votre suggestion? – StackOverflowNewbie

Répondre

70

Quick Fix, ajoutez dans vos options:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false) 

ou tout simplement l'ajouter à votre fonction actuelle:

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

Vous trouverez plus d'informations à ce sujet ici: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to- accès-https-ssltls-protected-sites/ – SystemX17

+0

désolé pour le mauvais montage ;-) Je n'ai pas compris ce que vous vouliez dire ... – RageZ

+0

c'est ok - je suis seulement nouveau ici depuis hier. merci d'avoir fait ce code mieux pour la personne qui demande - ur génial. – SystemX17

21

Je tentais d'utiliser CURL pour faire une API https appelle avec php et a couru dans ce problème. J'ai remarqué une recommandation sur le site php qui me leva et le fonctionnement: 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

Je l'ai fait mais maintenant je ne reçois que l'erreur '' 35: 14077410: routines SSL: SSL23_GET_SERVER_HELLO: sslv3 alert handshake failure'' – OZZIE

0

une autre option comme réponse Gavin Palmer est d'utiliser le fichier .pem mais avec une option boucle

1- télécharger la dernière mise à jour .pem fichier de https://curl.haxx.se/docs/caextract.html et enregistrez-le quelque part sur votre serveur (en dehors du dossier public)

2- définir l'option dans votre code au lieu du fichier php.ini

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