php require_once simple_html_dom php class SmsReceiver private ch cons

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
require_once 'simple_html_dom.php';
class SmsReceiver {
private $ch;
const PROVIDER = 'http://www.xn--d1abtbdbhslb5m.xn--p1ai'; // www.номердлясмс.рф
public function __construct() {
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
}
public function getNumbers($prefix) {
curl_setopt($this->ch, CURLOPT_URL, self::PROVIDER);
$res = curl_exec($this->ch);
$html = new simple_html_dom();
$html->load($res);
$items = $html->find('option');
$numbers = [];
foreach($items as $num) {
if(preg_match('/^\+'.$prefix.'/i', $num->plaintext)) {
preg_match('/^\+(\d+)/', $num->plaintext, $phone);
$numbers[] = ['phone' => $phone[1], 'id' => $num->value];
}
}
return $numbers;
}
public function getSms($phone_id) {
curl_setopt_array($this->ch, [
CURLOPT_URL => self::PROVIDER . '/json.php',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'ajax' => '1',
'number' => $phone_id
]
]);
$res = curl_exec($this->ch);
$html = new simple_html_dom();
$html->load($res);
$items = $html->find('tr');
$last_sms = [];
foreach($items as $sms) {
$last_sms[] = ['from' => $sms->children(0)->plaintext, 'text' => $sms->children(1)->plaintext];
}
print_r($last_sms);
}
}