رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
نمونه کدی برای وصل شدن و کار کرن با پنل های ارسال پیامک
#3
(29-12-1396، 12:26 ب.ظ)ADMIN نوشته: من طبق API سایت SMS.ir توضیح میدم که خودمون هم نماینده‌اش هستیم و درصورت تمایل میتونین ازطریق ایمیل به واحد فروش sales@ncis.ir درخواست بدین تا در اختیارتون قرار بگیره و بتونین ازطریق نشانی newsms.ncis.ir از این پنل استفاده کنید.

ابتدا یک پوشه‌ی components به پروژه‌ی خودتون اضافه کنید و بعد یک فایل جدید به‌اسم SMS.php ایجاد کنید و این کد رو داخلش بنویسید:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
 
namespace appcomponents;
 
use yiibaseComponent;
 
set_time_limit(0);
 
class SMS extends Component
{
    public $apiKey;
    public $secretKey;
    public $lineNumber;
 
    public function send($to, $text)
    {
        $token = $this->getToken();
        if ($token != false) {
            $postData = array(
                'Messages' => [$text],
                'MobileNumbers' => is_array($to) ? $to : [$to],
                'LineNumber' => $this->lineNumber,
                'SendDateTime' => '',
                'CanContinueInCaseOfError' => 'false',
            );
            $url = $this->getAPIMessageSendUrl();
            $SendMessage = $this->execute($postData, $url, $token);
            $object = json_decode($SendMessage);
            if (is_object($object)) {
                $array = get_object_vars($object);
                if (is_array($array)) {
                    $result = $array['Message'];
                } else {
                    $result = false;
                }
            } else {
                $result = false;
            }
 
        } else {
            $result = false;
        }
        return $result;
    }
 
    private function getToken()
    {
        $postData = array(
            'apiKey' => $this->apiKey,
            'SecretKey' => $this->secretKey,
            'System' => 'php_rest_v_1_1'
        );
        $postString = json_encode($postData);
 
        $ch = curl_init($this->getApiTokenUrl());
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        ));
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, count($postString));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
 
        $result = curl_exec($ch);
        curl_close($ch);
 
        $response = json_decode($result);
 
        $resp = false;
 
        if (is_object($response)) {
            $resultVars = get_object_vars($response);
            if (is_array($resultVars)) {
                @$IsSuccessful = $resultVars['IsSuccessful'];
                if ($IsSuccessful == true) {
                    @$TokenKey = $resultVars['TokenKey'];
                    $resp = $TokenKey;
                }
            }
        }
 
        return $resp;
    }
 
    private function getApiTokenUrl()
    {
        return 'http://RestfulSms.com/api/Token';
    }
 
    private function getAPIMessageSendUrl()
    {
    }
 
    private function execute($postData, $url, $token)
    {
 
        $postString = json_encode($postData);
 
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'x-sms-ir-secure-token: ' . $token
        ));
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, count($postString));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
 
        $result = curl_exec($ch);
        curl_close($ch);
 
        return $result;
    }
}

حالا توی فایل تنظیمات config/web.php این کامپوننت رو معرفی کنید:
1
2
3
4
5
6
7
8
9
'components' => [
    'sms' => [
        'class' => 'appcomponentsSMS',
        'apiKey' => '******',
        'secretKey' => '******',
        'lineNumber' => '******',
    ],
    // ...
],

بجای apiKey و secretKey و lineNumber هم باید مقادیر مربوط به پنل خودتون رو که در قسمت «API برنامه‌نویسان» میتونین تعریف کنین، وارد کنید.

حالا هرجای برنامه که خواستین پیام ارسال کنید، میتونین از این روش کمک بگیرین:
1
2
3
Yii::$app->sms->send($targetNumber, $message); // Single target
// or
Yii::$app->sms->send([$number1, $number2, $number3], $message); // Multiple targets

امیدوارم که روش کار رو متوجه شده باشین.


بسیار عالی تشکر
پاسخ
تشکر شده توسط:


پیام‌های این موضوع
RE: نمونه کدی برای وصل شدن و کار کرن با پنل های ارسال پیامک - narimant - 09-01-1397، 01:37 ب.ظ



کاربران در حال بازدید این موضوع: 1 مهمان