10/31/2017

PHP : PHP ดึง rate จาก Web กสิกรใหม่มาแสดงใน Internet

ดยใช้วิธีดึงค่าจาก aspx ของ ธนาคารเลย
https://www.kasikornbank.com/TH/Rate/Pages/Foreign-Exchange.aspx

https://stackoverflow.com/questions/24134886/get-html-code-from-aspx-page-with-php
  1. <?php
  2. include_once("simple_html_dom.php");
  3. $url = 'https://www.kasikornbank.com/TH/Rate/Pages/Foreign-Exchange.aspx';
  4. $contents = htmlentities(file_get_contents($url));
  5. echo $contents;
  6.  
  7. ?>


สามารถส่งวันที่ เข้าไปได้ ก็จะได้ค่าเช่นกัน

  1. <?php
  2. include_once("simple_html_dom.php");
  3. $url = 'https://www.kasikornbank.com/TH/Rate/Pages/Foreign-Exchange.aspx?d=19&m=05&y=2017&r=0';
  4. $contents = htmlentities(file_get_contents($url));
  5. echo $contents;
  6.  
  7. ?>



เอาค่ามาใช้งานโดย ใช้ explode <div> เอา
https://stackoverflow.com/questions/20446598/get-div-content-from-external-website

  1. <?php
  2. include_once("simple_html_dom.php");
  3. $url = 'https://www.kasikornbank.com/TH/Rate/Pages/Foreign-Exchange.aspx';
  4. //$contents = htmlentities(file_get_contents($url));
  5. //$contents  = file_get_contents($url);
  6. //echo $contents;
  7. //exit;
  8.  
  9. // FIND THE DESIRED DIV
  10. $htm = file_get_contents($url);
  11. echo htmlentities($htm);
  12.  
  13. $strDate = '<div id="divRate"';
  14. $arrDate = explode($strDate, $htm);
  15. $arrDate = explode('</div', $arrDate[1]);
  16.  
  17. // RECONSTRUCT THE DIV WITH HTML TAGS
  18. $new = $strDate . $arrDate[0] .'</div>';
  19.  
  20. echo htmlentities($new);
  21.  
  22. ?>


ตัวอย่าง Rate ข้อเสนอแนะคุณพเยาว์
https://intranet.scivalve.com/sci/AF/rate_bank.php
  1.  
  2. <?php
  3. error_reporting(E_ERROR);
  4. define('IN_PHPBB', true);
  5. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../../';
  6. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  7. include($phpbb_root_path . 'common.' . $phpEx);
  8.  
  9. include($phpbb_root_path ."sci/include/config.php");
  10. //include($phpbb_root_path ."sci/include/db.php");
  11. include($phpbb_root_path ."sci/include/functions.php");
  12. //include($phpbb_root_path ."sci/include/db2.php");
  13.  
  14. include_once("simple_html_dom.php");
  15.  
  16. //$url = 'https://www.kasikornbank.com/TH/Rate/Pages/Foreign-Exchange.aspx?d=19&m=05&y=2017&r=0';
  17. $url = 'https://www.kasikornbank.com/TH/Rate/Pages/Foreign-Exchange.aspx';
  18. //$contents = htmlentities(file_get_contents($url));
  19. //$contents  = file_get_contents($url);
  20. //echo $contents;
  21. //exit;
  22.  
  23. // FIND THE DESIRED DIV
  24. $htm = file_get_contents($url);
  25. echo htmlentities($htm);
  26. /*
  27. $strDate = '<div id="divRate"';
  28. $arrDate = explode($strDate, $htm);
  29. $arrDate = explode('</div', $arrDate[1]);
  30.  
  31. // RECONSTRUCT THE DIV WITH HTML TAGS
  32. $new = $strDate . $arrDate[0] .'</div>';
  33.  
  34. echo htmlentities($new);
  35. */
  36.  
  37. //########### USD ############//
  38. $strDiv = '<div id="divLastRate"';
  39. $arrDiv = explode($strDiv, $htm);
  40.  
  41. $arrDiv = explode('</div>', $arrDiv[1]);
  42.  
  43. // RECONSTRUCT THE DIV WITH HTML TAGS
  44. $newDiv = $strDiv . $arrDiv[0] .'</div>';
  45.  
  46. //echo htmlentities($newDiv);
  47.  
  48. // Explode Data For Show Buying.
  49. $strBuyUSD = 'data-BuyTelex="';
  50. $arrBuyUSD = explode($strBuyUSD, $newDiv);
  51. $arrBuyUSD = explode('"', $arrBuyUSD[1]);
  52. $newBuyUSD = $arrBuyUSD[0];
  53.  
  54. //echo $newBuyUSD;
  55.  
  56. // Explode Data For Show Selling.
  57. $strSellUSD = 'data-SellChq="';
  58. $arrSellUSD = explode($strSellUSD, $newDiv);
  59. $arrSellUSD = explode('"', $arrSellUSD[1]);
  60. $newSellUSD = $arrSellUSD[0];
  61.  
  62. //echo $newSellUSD;
  63. //########### USD ############//
  64.  
  65. //########### CNY ############//
  66. $strDiv = '<div id="divLastRate"';
  67. $arrDiv = explode($strDiv, $htm);
  68. $arrDiv = explode('</div>', $arrDiv[1]);
  69.  
  70. // RECONSTRUCT THE DIV WITH HTML TAGS
  71. $newDiv = $strDiv . $arrDiv[10] .'</div>';
  72.  
  73. //echo htmlentities($newDiv);
  74.  
  75. // Explode Data For Show Buying.
  76. $strBuyCNY = 'data-BuyTelex="';
  77. $arrBuyCNY = explode($strBuyCNY, $newDiv);
  78. $arrBuyCNY = explode('"', $arrBuyCNY[1]);
  79. $newBuyCNY = $arrBuyCNY[0];
  80.  
  81. //echo $newBuyCNY;
  82.  
  83. // Explode Data For Show Selling.
  84. $strSellCNY = 'data-SellChq="';
  85. $arrSellCNY = explode($strSellCNY, $newDiv);
  86. $arrSellCNY = explode('"', $arrSellCNY[1]);
  87. $newSellCNY = $arrSellCNY[0];
  88.  
  89. //echo $newSellCNY;
  90. //########### CNY ############//
  91.  
  92. //########### EUR ############//
  93. $strDiv = '<div id="divLastRate"';
  94. $arrDiv = explode($strDiv, $htm);
  95. $arrDiv = explode('</div>', $arrDiv[1]);
  96.  
  97. // RECONSTRUCT THE DIV WITH HTML TAGS
  98. $newDiv = $strDiv . $arrDiv[12] .'</div>';
  99.  
  100. //echo htmlentities($newDiv);
  101.  
  102. // Explode Data For Show Buying.
  103. $strBuyEUR = 'data-BuyTelex="';
  104. $arrBuyEUR = explode($strBuyEUR, $newDiv);
  105. $arrBuyEUR = explode('"', $arrBuyEUR[1]);
  106. $newBuyEUR = $arrBuyEUR[0];
  107.  
  108. //echo $newBuyEUR;
  109.  
  110. // Explode Data For Show Selling.
  111. $strSellEUR = 'data-SellChq="';
  112. $arrSellEUR = explode($strSellEUR, $newDiv);
  113. $arrSellEUR = explode('"', $arrSellEUR[1]);
  114. $newSellEUR = $arrSellEUR[0];
  115.  
  116. //echo $newSellEUR;
  117. //########### EUR ############//
  118.  
  119. //########### GBP ############//
  120. $strDiv = '<div id="divLastRate"';
  121. $arrDiv = explode($strDiv, $htm);
  122. $arrDiv = explode('</div>', $arrDiv[1]);
  123.  
  124. // RECONSTRUCT THE DIV WITH HTML TAGS
  125. $newDiv = $strDiv . $arrDiv[13] .'</div>';
  126.  
  127. //echo htmlentities($newDiv);
  128.  
  129. // Explode Data For Show Buying.
  130. $strBuyGBP = 'data-BuyTelex="';
  131. $arrBuyGBP = explode($strBuyGBP, $newDiv);
  132. $arrBuyGBP = explode('"', $arrBuyGBP[1]);
  133. $newBuyGBP = $arrBuyGBP[0];
  134.  
  135. //echo $newBuyGBP;
  136.  
  137. // Explode Data For Show Selling.
  138. $strSellGBP = 'data-SellChq="';
  139. $arrSellGBP = explode($strSellGBP, $newDiv);
  140. $arrSellGBP = explode('"', $arrSellGBP[1]);
  141. $newSellGBP = $arrSellGBP[0];
  142.  
  143. //echo $newSellGBP;
  144. //########### GBP ############//
  145.  
  146. //########### HKD ############//
  147. $strDiv = '<div id="divLastRate"';
  148. $arrDiv = explode($strDiv, $htm);
  149. $arrDiv = explode('</div>', $arrDiv[1]);
  150.  
  151. // RECONSTRUCT THE DIV WITH HTML TAGS
  152. $newDiv = $strDiv . $arrDiv[14] .'</div>';
  153.  
  154. //echo htmlentities($newDiv);
  155.  
  156. // Explode Data For Show Buying.
  157. $strBuyHKD = 'data-BuyTelex="';
  158. $arrBuyHKD = explode($strBuyHKD, $newDiv);
  159. $arrBuyHKD = explode('"', $arrBuyHKD[1]);
  160. $newBuyHKD = $arrBuyHKD[0];
  161.  
  162. //echo $newBuyHKD;
  163.  
  164. // Explode Data For Show Selling.
  165. $strSellHKD = 'data-SellChq="';
  166. $arrSellHKD = explode($strSellHKD, $newDiv);
  167. $arrSellHKD = explode('"', $arrSellHKD[1]);
  168. $newSellHKD = $arrSellHKD[0];
  169.  
  170. //echo $newSellHKD;
  171. //########### HKD ############//
  172.  
  173. //########### JPY ############//
  174. $strDiv = '<div id="divLastRate"';
  175. $arrDiv = explode($strDiv, $htm);
  176. $arrDiv = explode('</div>', $arrDiv[1]);
  177.  
  178. // RECONSTRUCT THE DIV WITH HTML TAGS
  179. $newDiv = $strDiv . $arrDiv[18] .'</div>';
  180.  
  181. //echo htmlentities($newDiv);
  182.  
  183. // Explode Data For Show Buying.
  184. $strBuyJPY = 'data-BuyTelex="';
  185. $arrBuyJPY = explode($strBuyHKD, $newDiv);
  186. $arrBuyJPY = explode('"', $arrBuyHKD[1]);
  187. $newBuyJPY = $arrBuyHKD[0];
  188.  
  189. //echo $newBuyJPY;
  190.  
  191. // Explode Data For Show Selling.
  192. $strSellJPY = 'data-SellChq="';
  193. $arrSellJPY = explode($strSellJPY, $newDiv);
  194. $arrSellJPY = explode('"', $arrSellJPY[1]);
  195. $newSellJPY = $arrSellJPY[0];
  196.  
  197. //echo $newSellJPY;
  198. //########### JPY ############//
  199.  
  200. //########### MYR ############//
  201. $strDiv = '<div id="divLastRate"';
  202. $arrDiv = explode($strDiv, $htm);
  203. $arrDiv = explode('</div>', $arrDiv[1]);
  204.  
  205. // RECONSTRUCT THE DIV WITH HTML TAGS
  206. $newDiv = $strDiv . $arrDiv[24] .'</div>';
  207.  
  208. //echo htmlentities($newDiv);
  209.  
  210. // Explode Data For Show Buying.
  211. $strBuyMYR = 'data-BuyTelex="';
  212. $arrBuyMYR = explode($strBuyHKD, $newDiv);
  213. $arrBuyMYR = explode('"', $arrBuyMYR[1]);
  214. $newBuyMYR = $arrBuyMYR[0];
  215.  
  216. //echo $newBuyMYR;
  217.  
  218. // Explode Data For Show Selling.
  219. $strSellMYR = 'data-SellChq="';
  220. $arrSellMYR = explode($strSellMYR, $newDiv);
  221. $arrSellMYR = explode('"', $arrSellMYR[1]);
  222. $newSellMYR = $arrSellMYR[0];
  223.  
  224. //echo $newSellMYR;
  225. //########### MYR ############//
  226.  
  227. //########### SGD ############//
  228. $strDiv = '<div id="divLastRate"';
  229. $arrDiv = explode($strDiv, $htm);
  230. $arrDiv = explode('</div>', $arrDiv[1]);
  231.  
  232. // RECONSTRUCT THE DIV WITH HTML TAGS
  233. $newDiv = $strDiv . $arrDiv[24] .'</div>';
  234.  
  235. //echo htmlentities($newDiv);
  236.  
  237. // Explode Data For Show Buying.
  238. $strBuySGD = 'data-BuyTelex="';
  239. $arrBuySGD = explode($strBuySGD, $newDiv);
  240. $arrBuySGD = explode('"', $arrBuySGD[1]);
  241. $newBuySGD = $arrBuySGD[0];
  242.  
  243. //echo $newBuySGD;
  244.  
  245. // Explode Data For Show Selling.
  246. $strSellSGD = 'data-SellChq="';
  247. $arrSellSGD = explode($strSellSGD, $newDiv);
  248. $arrSellSGD = explode('"', $arrSellSGD[1]);
  249. $newSellSGD = $arrSellSGD[0];
  250.  
  251. //echo $newSellSGD;
  252. //########### SGD ############//
  253.  
  254. ?>
  255.  

No comments:

Post a Comment