2010-12-07 22 views
43

ich diese Funktion gefunden, die einen tollen Job macht (IMHO): http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curlPHP CURL & 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; 
} 

Das einzige Problem, das ich habe ist, dass es nicht für https funktioniert: //. Anny Ideen, was ich tun muss, damit dies für https funktioniert? Vielen Dank!

+4

define "funktioniert nicht" bitte. –

+3

curl per default Überprüfen Sie, ob das SSL-Zertifikat gültig ist ... Sie möchten dieses Verhalten möglicherweise deaktivieren, wenn Sie das betreffende Zertifikat selbst signiert haben – RageZ

+0

@RegeZ - Wie machen Sie Ihren Vorschlag? – StackOverflowNewbie

Antwort

70

Quick Fix, fügen Sie diese in Ihre Optionen:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false) 

oder fügen Sie ihn einfach, um Ihre aktuelle Funktion:

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

Hier gibt es weitere Informationen: http://unitestep.net/blog/2009/05/05/using-curl-in-php-to- access-https-ssltls-protected-sites/ – SystemX17

+0

sorry für den falschen Schnitt ;-) Ich habe nicht verstanden was du meinst ... – RageZ

+0

das ist ok - ich bin erst seit gestern neu hier. Danke, dass du den Code für die Person, die fragst, besser gemacht hast. – SystemX17

21

ich versuchte ROTATION zu verwenden, um einige https API mit PHP ruft zu tun und stieß auf dieses Problem. Ich bemerkte auf der PHP-Website eine Empfehlung, die mich zum Laufen bekam: 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

Ich tat dies, aber jetzt bekomme ich nur "35 Fehler: 14077410: SSL-Routinen: SSL23_GET_SERVER_HELLO: sslv3 Alarm Handshake Fehler" – OZZIE

0

eine andere Option wie Gavin Palmer Antwort ist die .pem Datei zu verwenden, aber mit einer Locke Option

1- Downloads die letzten Aktualisierung .pem Datei aus https://curl.haxx.se/docs/caextract.html und es irgendwo auf Ihrem Server (außerhalb des öffentlichen Ordner) speichern

2- die Option im Code statt die Datei php.ini

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