Advertisement
Guest User

Untitled

a guest
Oct 17th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. require_once('classes/class.smsquick.php');
  2.  
  3. $username = "someuser";
  4. $password = "somepass";
  5.  
  6. $api = new SmsQuick($username, $password);
  7.  
  8. //uncommenting the line below returns undefined_index @ line 312 and 282
  9. //$available_credits = $api->checkBalance();
  10.  
  11. //var_dump($api); //used for checking
  12.  
  13. public function checkBalance() {
  14. $vars = array(
  15. 'username' => $this->api_username,
  16. 'password' => $this->api_password,
  17. 'action' => 'balance',
  18. );
  19.  
  20. $retval = $this->executeApiRequest($vars);
  21. list(, $response) = array_values(reset($retval)); // line 282
  22.  
  23. return (int) $response;
  24. }
  25.  
  26. /**
  27. * Helper method to execute an API request.
  28. *
  29. * @param array $vars
  30. * Data to POST to SMS gateway API endpoint.
  31. *
  32. * @return array
  33. * Response from SMS gateway.
  34. */
  35. public function executeApiRequest($vars) {
  36. // Basic validation on the authentication details
  37. foreach ($vars as $key => $value) {
  38. switch ($key) {
  39. case 'username':
  40. case 'password':
  41. if (empty($value)) {
  42. throw new Exception('API username or password not specified.');
  43. }
  44. break;
  45. }
  46. }
  47.  
  48. $data = $this->preparePostData($vars);
  49. $retval = $this->executePostRequest($data);
  50.  
  51. list($status, $response) = explode(':', $retval); // line 312
  52. if ($status == 'ERROR') {
  53. throw new Exception(strtr('There was an error with this request: !error.', array('!error' => $response)));
  54. }
  55.  
  56. $data = array();
  57. $lines = explode("n", $retval);
  58. foreach (array_filter($lines) as $i => $line) {
  59. $line = trim($line);
  60. $data[$i] = explode(':', $line);
  61. }
  62.  
  63. return $data;
  64. }
  65.  
  66. protected function preparePostData($data) {
  67. $post_data = array();
  68. foreach ($data as $key => $value) {
  69. switch ($key) {
  70. case 'to':
  71. // Support multiple phone numbers.
  72. $value = implode(',', array_unique($value));
  73. break;
  74. }
  75. $post_data[] = $key . '=' . rawurlencode($value);
  76. }
  77.  
  78. return implode('&', $post_data);
  79. }
  80.  
  81. protected function executePostRequest($data) {
  82. $ch = curl_init($this->api_endpoint);
  83. curl_setopt($ch, CURLOPT_POST, TRUE);
  84. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  85. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  86. $retval = curl_exec($ch);
  87. curl_close($ch);
  88. return $retval;
  89. }
  90.  
  91. reset($retval);
  92. list(, $response) = array_values(reset($retval)); // line 282
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement