12/28/2016

PHP : PHP ดึงค่าจาก Web HTTPS มาใช้งาน ด้วย PHP Simple HTML DOM Parser Manual

PHP : PHP ดึงค่าจาก Web HTTPS มาใช้งาน ด้วย PHP Simple HTML DOM Parser Manual

http://simplehtmldom.sourceforge.net/manual.htm

ถ้าดึง Web ปกติ สามารถใช้ ได้เลย
  1. include('simple_html_dom.php');
  2. $dom = file_get_html("http://www.kasikornbank.com/TH/RatesAndFees/ForeignExchange/Pages/ForeignExchangeHistory.aspx?");
  3. $html = $dom->find('table[class=ContentTB] tbody tr');
  4. foreach ($html as $tr){
  5. echo "<br><font color='red'>".$tr."</font>";
  6. }


แต่ถ้าเป็น HTTPS จะไม่ได้จะมี Error
Warning: file_get_contents(): SSL: Connection reset by peer in Line Error
Warning: file_get_contents(): Failed to enable crypto in Line Error

ต้องใช้ เพื่อให้ https ผ่าน
  1. include('simple_html_dom.php');
  2. $url = 'https://www.pbebank.com/rates/forex.html';
  3.  
  4. $arrContextOptions=array(
  5.     "ssl"=>array(
  6.         "verify_peer"=>false,
  7.         "verify_peer_name"=>false,
  8.     ),
  9. );  
  10.  
  11. stream_context_create($arrContextOptions));
  12. $response = file_get_contents("https://www.pbebank.com/rates/forex.html", false, stream_context_create($arrContextOptions));
  13.  
  14. print_r($response);
  15.  
  16. $dom = str_get_html($response);
  17. $html = $dom->find('table[id=tech-companies] tbody tr');
  18.  
  19. print_r("<br>".$html[1]);
  20. print_r("<br>".$html[3]);

No comments:

Post a Comment