jesobreira

FreeSMS4Us Get SMS

Mar 14th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?php
  2. /**
  3.     @description A function to receive SMS from FreeSMS4Us.com. It returns an array with all SMS received.
  4.     @author Jefrey S. Santos <jefreysobreira[at]gmail[dot]com>
  5.     @note There's no way to send SMS by PHP (by that website) because of the damn (but creative) captcha.
  6. */
  7. function freesms4us_get($number) {
  8.     $ch = curl_init();
  9.     curl_setopt($ch, CURLOPT_URL, "http://freesms4us.com/reply.php");
  10.     curl_setopt($ch, CURLOPT_POST, true);
  11.     curl_setopt($ch, CURLOPT_POSTFIELDS, "nomer=$number");
  12.     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
  13.     curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  14.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  15.     $store = curl_exec($ch);
  16.     $store = strip_tags($store);
  17.  
  18.     $store = explode("\n", $store);
  19.     $store = array_map("trim", $store);
  20.  
  21.     $msgs = array();
  22.  
  23.     foreach($store as $line) {
  24.         $pattern = "/^SMS no:\-[0-9]*\. [&nbsp;]{4}(.*) \)(.*)$/";
  25.         if(preg_match($pattern, $line)) {
  26.             $res = array();
  27.             preg_match_all($pattern, $line, $res);
  28.             if(count($res)) {
  29.                 $msgs[] = $res[2][0];
  30.             }
  31.         }
  32.     }
  33.  
  34.     return $msgs;
  35. }
  36.  
  37. // Example of use:
  38. $number = "000000000000"; // no need +62, just start with 0
  39. $sms = freesms4us_get($number);
  40. foreach($sms as $message) {
  41.    echo $message."<br/>"; // display the message
  42. }
  43.  
  44. // Example #2: Multiple numbers
  45. $numbers = array(
  46.     "John Doe" => "000000000000",
  47.     "Jane Roe" => "000000000000" // no comma after the last number
  48. );
  49.  
  50. foreach($numbers as $number) { // loop for numbers
  51.     echo "SMS from ".$number;
  52.     $sms = freesms4us_get($number);
  53.     echo "<ul>";
  54.     foreach($sms as $message) { // loop for messages
  55.         echo "<li>";
  56.         echo $message;
  57.         echo "</li>";
  58.     }
  59.     echo "</ul>";
  60.     echo "<hr/>";
  61. }
Add Comment
Please, Sign In to add comment