Advertisement
OreozDevelopers

REST

Oct 21st, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>REST API</title>
  6. </head>
  7. <body>
  8.  
  9. <?php
  10.  
  11. $gas = $_GET['gas'];
  12.  
  13. if (isset($gas)) {
  14. sendDb();
  15. sendNetpie();
  16.  
  17. if ($gas >= 50.0){ //Alert Val
  18. sendLine();
  19. sendIFTTT();
  20. }else{
  21. echo "Val";
  22. }
  23. }
  24.  
  25. function sendDb(){
  26.  
  27. $gas = $_GET['gas'];
  28.  
  29. $dbcon = mysqli_connect('localhost','root','','gasdb');
  30. mysqli_set_charset($dbcon,'utf8');
  31.  
  32. $query = "INSERT INTO data (gas) VALUES ('$gas')";
  33.  
  34. $result = mysqli_query($dbcon,$query);
  35. mysqli_close($dbcon);
  36. }
  37.  
  38. function sendNetpie(){
  39. $url = 'https://api.netpie.io/topic/GAS/HOME/LED?retain';
  40. $username = "Y99FEk47zUqcwes";
  41. $password = "1UmlGSWmEtn9SCQXVFDqy3z7N";
  42.  
  43. $data = $_GET['gas'];
  44.  
  45. $ch = curl_init($url);
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  47. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  48. curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
  49. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  50. $response = curl_exec($ch);
  51. }
  52.  
  53. function sendLine(){
  54. $api = "https://notify-api.line.me/api/notify";
  55. $token = "SvUDJlRNIPC25mLn31idNlEupAEIdXRPQIOFHfDMAAr";
  56. $msg = "ขณะนี้ GAS มีค่า".$_GET['gas']." หน่วย";
  57.  
  58. $ch = curl_init($api);
  59. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  60. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  61. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded","Authorization: Bearer " .$token));
  62. curl_setopt($ch, CURLOPT_POSTFIELDS,"message=$msg");
  63. $response = curl_exec($ch);
  64. }
  65.  
  66. function sendIFTTT(){
  67. $api = "https://maker.ifttt.com/trigger/TEST/with/key/nTw8bZmfR-dtgOgl4TvUqO1U12mv9o7XQbYZmn5k_5O";
  68. $data = '{"value1":"'.$_GET['gas'].'"}';
  69.  
  70. $ch = curl_init($api);
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  72. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  73. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
  74. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  75. $response = curl_exec($ch);
  76. }
  77.  
  78.  
  79.  
  80.  
  81. ?>
  82. </body>
  83. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement