php獲取https協議內容的兩種方法

第一種:

 

直接使用函數:

 

file_get_contents();

 

前提是需要php編譯時候支持 ssl,也就是加上with=openssl選項,windows下則需要將extention=openssl.dll選項打開。

 

比如:echo file_get_contents('https://www.paypal.com/c2');即可打印出結果!

 

 

 

 


第二種:

 

 

使用CURL,當然你的php需要支持curl,linux中需要配置。。。,windows中需要配置。。。。(省略)

 

方法:

ob_start();

$url = ('https://www.paypal.com/c2');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$result = curl_exec($ch);

 

$r = ob_get_contents();

 

echo $r;

 

 

原理是將內容打印到緩衝區,然後將緩衝區內容賦值給一個變量。

 

 

 

 

 

 

 


 

 

參考資料:

php支持curl擴展:

http://developer.51cto.com/art/200904/121739.htm

http://php.net/manual/en/function.file-get-contents.php

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章