Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [klasa] Powiadamianie sms
marcinpruciak
post 20.09.2008, 20:44:26
Post #1





Grupa: Zarejestrowani
Postów: 161
Pomógł: 9
Dołączył: 14.07.2008

Ostrzeżenie: (0%)
-----


Przedstawiam Wam moją pierwszą klasę, którą wykonałem. Ma ona za zadanie pobrać nieczytane emaile i wysłać ich tytuły na komórkę przez sms. Sprawa bardzo przydatna. 

Nie jest to jakiś super profesionalny kod, ale działa.

  1. <? 
  2. class Baza {
  3.  public $polacz;
  4.  public $host = "localhost";
  5.     public $baza = "powiadamiania";
  6.     public $user = "xxxx";
  7.     public $password = "xxxxx"; 
  8.  public $table;
  9.  
  10.      public function __construct() 
  11.  {
  12.         $this->polacz = mysqli_connect($this->host, $this->user, $this->password, $this->baza) OR trigger_error('Nie można połaczyć z bazą danych');
  13.      $this->polacz -> query("SET NAMES 'latin2'");
  14.     }
  15.  
  16.  public function query($q)
  17.  {
  18.      return mysqli_query($this->polacz, $q);
  19.  }
  20.  
  21.  public function check($data)
  22.  {
  23.      $q="SELECT * FROM `emails` WHERE `data`='$data'";
  24.      $r = $this->query($q);
  25.      $this->table = mysqli_fetch_array($r);
  26.  }
  27.  
  28.  public function insert($data, $subject)
  29.  {
  30.      $q="INSERT INTO `emails` (`id`, `data`, `temat`) VALUES ('' , '$data', '$subject')";
  31.      $r = $this->query($q); 
  32.  }
  33. } 
  34.  
  35. class Imap
  36. {
  37.  public $mailbox;
  38.  public $username;
  39.  public $password;
  40.  public $type;
  41.  public $header;
  42.  public $subject;
  43.  public $data;
  44.  public $imap;
  45.  
  46.  public function GetEmails()
  47.  {
  48.      $this->imap = imap_open($this->mailbox, $this->username, $this->password);
  49.      return $emails = imap_search($this->imap, $this->type);
  50.  }
  51.  
  52.  public function GetHeader($email)
  53.  {
  54.      $this->header = imap_headerinfo($this->imap, $email);
  55.      $this->subject = imap_utf8($this->header->subject);
  56.      $this->data = $this->header->date;
  57.      print_r($this->imap->subject);
  58.  }
  59. }
  60.  
  61. class SendSms
  62. {
  63.  public $url;
  64.  public $data;
  65.  public $r;
  66.  
  67.  public function curl()
  68.  {
  69.      $curl = curl_init($this->url);
  70.      curl_setopt($curl, CURLOPT_FAILONERROR, 1);
  71.      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  72.      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  73.      curl_setopt($curl, CURLOPT_TIMEOUT, 50);
  74.      curl_setopt($curl, CURLOPT_POST, 1);
  75.      curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data); 
  76.      $this->r = curl_exec($curl); 
  77.      curl_errno($curl); 
  78.      curl_error($curl); 
  79.      curl_close($curl);
  80.      if(isset($this->r)) {
  81.          return true;
  82.      }
  83.  }
  84.  
  85.  public function ShowRezults()
  86.  {
  87.      return '<h2>cURL wyni:</h2><pre>'.htmlentities($this->r).'/</pre>';
  88.  }
  89. }
  90.  
  91. class SendTitleSms
  92. {
  93.  public $imap;
  94.  public $baza;
  95.  public $sms;
  96.  
  97.  public function __construct()
  98.  {
  99.      $this->imap = new Imap;
  100.      $this->baza = new Baza;
  101.      $this->sms = new SendSms;
  102.  }
  103.  
  104.  public function SetData($mailbox, $username, $pass, $type, $tprefix, $number)
  105.  {
  106.      $this->imap->mailbox = $mailbox;
  107.      $this->imap->username = $username;
  108.      $this->imap->password = $pass;
  109.      $this->imap->type = $type;
  110.   
  111.      $this->sms->url = 'ht://www.text.plusgsm.pl/sms/sendsms.php';
  112.      $this->sms->data = 'tprefix='.$tprefix.'&numer='.$number.'&odkogo=mail&tekst=';
  113.   
  114.  }
  115.   
  116.  public function send()
  117.  {
  118.      $emails = $this->imap->GetEmails();
  119.      if(!empty($emails)) {
  120.          foreach ($emails as $email) {
  121.              $this->imap->GetHeader($email);
  122.              $this->baza->check($this->imap->data);
  123.                  if($this->baza->table['data'] == $this->imap->data) {
  124.                      echo 'Ta wiadomość już jest w bazie';
  125.                  } else {
  126.                      $this->sms->data .= $this->imap->subject;
  127.                      if($this->sms->curl()) {
  128.                          $this->baza->insert($this->imap->data, $this->imap->subject);
  129.                          echo $this->sms->ShowRezults();
  130.                          print_r($this->sms->data);
  131.                      }
  132.                       
  133.                  }
  134.          }
  135.      }
  136.  }
  137. }
  138. ?>


Wymagana jest jeszcze baza danych
Kod
CREATE TABLE `emails` (
  `id` int(11) NOT NULL auto_increment,
  `data` varchar(40) NOT NULL,
  `temat` varchar(50) character set utf8 collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=109;

A tu jej uzycie

  1. <?php
  2. $send = new SendTitleSms;
  3. $send->SetData("{imap.gmail.co:993/imap/ssl}INBOX", 'email', 'haslo', "UNSEEN", 'prefix', 'numertelefonu');
  4. $send->send();
  5. ?>



Klasa ta działa tylko na telefon w plusie i ma dzienny limit bramki chyba 10 sms, ale mi to wystarcza. 

Proszę o ocenę.

Ten post edytował marcinpruciak 20.09.2008, 20:47:23


--------------------
Go to the top of the page
+Quote Post
Speedy
post 22.09.2008, 01:45:44
Post #2





Grupa: Zarejestrowani
Postów: 651
Pomógł: 28
Dołączył: 4.12.2004

Ostrzeżenie: (0%)
-----


Pamiętam, że kiedyś w plusie można było wysłać sms-a za pomocą e-maila wysyłając wiadomość na adres: numer-telefonu@plusgm... (nie pamiętam dokładnie adresu), więc w tym skrypcie mógłbyś obyć się bez tej bramki.
ps. Już myślałem, że ktoś wymyślił jakieś cwany sposób na wysyłanie sms-ów z internetu, a tu lipa tongue.gif. Tylko plus jest taki uprzejmy, że nie ma z nim większych problemów smile.gif.


--------------------
Sygnatura niezgodna z regulaminem.
Go to the top of the page
+Quote Post
vokiel
post 22.09.2008, 14:12:42
Post #3





Grupa: Zarejestrowani
Postów: 2 592
Pomógł: 445
Dołączył: 12.03.2007

Ostrzeżenie: (0%)
-----


Dobry pomysł, też mam tel w plusie smile.gif
BTW Wysyłanie maila jako sms: +48nrtel@text.plusgsm.pl


--------------------
Go to the top of the page
+Quote Post
Krizis
post 25.10.2008, 13:27:19
Post #4





Grupa: Zarejestrowani
Postów: 49
Pomógł: 1
Dołączył: 27.07.2008

Ostrzeżenie: (0%)
-----


A umiał byś zrobić coś takiego ze wysyla sms z wybrana przezemnie trescia?
Go to the top of the page
+Quote Post
prz3kus
post 4.11.2008, 21:38:02
Post #5





Grupa: Zarejestrowani
Postów: 260
Pomógł: 30
Dołączył: 22.01.2007

Ostrzeżenie: (0%)
-----


sorki ze troszke nie na temat ale czy jest mozliwosc wysyłania smsów z interneu automatem tak aby koszt za ich wysłanie naliczał mi się na moje konto telefonu. Chodzi mi o wysyłanie masowe
Go to the top of the page
+Quote Post
pest
post 5.11.2008, 08:16:20
Post #6





Grupa: Zarejestrowani
Postów: 78
Pomógł: 15
Dołączył: 10.12.2007
Skąd: Lublin

Ostrzeżenie: (0%)
-----


Takie pytanie to mógłbyś zadać u swojego operatora, czy udostępnia jakieś API do np. wysyłania smsów z jego płatnej bramki.

Później mógłbyś zdać relację na forum, na pewno by się przydało niejednemu winksmiley.jpg
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 27.04.2025 - 08:35