2010-12-07 13 views
43

मैं इस समारोह है कि एक भयानक काम (IMHO) करता पाया: http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_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; 
} 

समस्या सिर्फ मेरे पास है कि यह https के लिए काम नहीं करता है: //। एनी विचार क्या मुझे https के लिए यह काम करने के लिए क्या करना है? धन्यवाद!

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false) 

अथवा केवल अपने वर्तमान समारोह में जोड़ें::

+4

परिभाषित करें "काम नहीं करता" कृपया। डिफ़ॉल्ट रूप से –

+3

कर्ल जांचें कि क्या SSL प्रमाणपत्र मान्य है ... यदि आप – RageZ

+0

@RegeZ में प्रमाण पत्र पर हस्ताक्षर किए हैं तो आप उस व्यवहार को अक्षम करना चाहेंगे - अपना सुझाव कैसे करें? – StackOverflowNewbie

उत्तर

70

त्वरित सुधार, अपने विकल्पों में इस ऐड

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

यहां इस बारे में अधिक जानकारी है: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to- एक्सेस-https-ssltls-संरक्षित-साइट्स/ – SystemX17

+0

गलत संपादन के लिए खेद है ;-) मुझे आपका मतलब नहीं मिला ... – RageZ

+0

यह ठीक है - मैं कल से ही नया हूं। उस कोड को व्यक्ति के लिए बेहतर बनाने के लिए धन्यवाद - आप कमाल है। – SystemX17

21

मैं कर्ल का उपयोग करने के कुछ https एपीआई करने के लिए कोशिश कर रहा था php के साथ कहता है और इस समस्या में भाग गया। मैं php साइट है जो मुझे और मिल चल रहा पर एक सिफारिश देखा: 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

मैंने यह किया लेकिन अब मुझे केवल '35 त्रुटि मिलती है: 14077410: एसएसएल दिनचर्या: SSL23_GET_SERVER_HELLO: sslv3 अलर्ट हैंडशेक विफलता' – OZZIE

0

गेविन पामर उत्तर की तरह एक और विकल्प .pem फ़ाइल का उपयोग करने, लेकिन एक कर्ल विकल्प के साथ

1- अंतिम अपडेट डाउनलोड है https://curl.haxx.se/docs/caextract.html से .pem फ़ाइल और यह (सार्वजनिक फ़ोल्डर के बाहर) आपके सर्वर पर कहीं बचाने

2- बजाय php.ini फ़ाइल अपने कोड में विकल्प सेट

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