Guest User

checkconnection

a guest
Sep 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. <?php
  2. $from = "";
  3. $to = "";
  4. $did = "";
  5.  
  6. $password = "ClueCon";
  7. $port = "8021";
  8. $host = "127.0.0.1";
  9.  
  10. function event_socket_create($host, $port, $password) {
  11. $fp = fsockopen($host, $port, $errno, $errdesc)
  12. or die("Connection to $host failed");
  13. socket_set_blocking($fp,false);
  14.  
  15. if ($fp) {
  16. while (!feof($fp)) {
  17. $buffer = fgets($fp, 1024);
  18. usleep(100); //allow time for reponse
  19. if (trim($buffer) == "Content-Type: auth/request") {
  20. fputs($fp, "auth $password\n\n");
  21. break;
  22. }
  23. }
  24. return $fp;
  25. }
  26. else {
  27. return false;
  28. }
  29. }
  30.  
  31.  
  32. function event_socket_request($fp, $cmd) {
  33.  
  34. if ($fp) {
  35. fputs($fp, $cmd."\n\n");
  36. usleep(100); //allow time for reponse
  37.  
  38. $response = "";
  39. $i = 0;
  40. $contentlength = 0;
  41. while (!feof($fp)) {
  42. $buffer = fgets($fp, 4096);
  43. if ($contentlength > 0) {
  44. $response .= $buffer;
  45. }
  46.  
  47. if ($contentlength == 0) { //if contentlenght is already don't process again
  48. if (strlen(trim($buffer)) > 0) { //run only if buffer has content
  49. $temparray = split(":", trim($buffer));
  50. if ($temparray[0] == "Content-Length") {
  51. $contentlength = trim($temparray[1]);
  52. }
  53. }
  54. }
  55.  
  56. usleep(100); //allow time for reponse
  57.  
  58. //optional because of script timeout //don't let while loop become endless
  59. if ($i > 10000) { break; }
  60.  
  61. if ($contentlength > 0) { //is contentlength set
  62. //stop reading if all content has been read.
  63. if (strlen($response) >= $contentlength) {
  64. break;
  65. }
  66. }
  67. $i++;
  68. }
  69.  
  70. return $response;
  71. }
  72. else {
  73. echo "no handle";
  74. }
  75. }
  76.  
  77. $fp = event_socket_create($host, $port, $password);
  78.  
  79. $cmd = "api reloadxml";
  80.  
  81. $response = event_socket_request($fp, $cmd);
  82. echo $cmd; echo $response;
  83. fclose($fp);
  84.  
  85. ?>
Add Comment
Please, Sign In to add comment