|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MahdiMajidzadeh\kavenegar; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | + |
| 7 | +class Kavenegar |
| 8 | +{ |
| 9 | + |
| 10 | + private $base_url; |
| 11 | + |
| 12 | + public function __construct() |
| 13 | + { |
| 14 | + $this->base_url = config('kavenegar.base_url').config('kavenegar.key').'/'; |
| 15 | + } |
| 16 | + |
| 17 | + public function send($receptor, $message, $sender = null, $date = null, $type = null, $localid = null) |
| 18 | + { |
| 19 | + if (is_array($receptor)) { |
| 20 | + $receptor = implode(",", $receptor); |
| 21 | + } |
| 22 | + if (is_array($localid)) { |
| 23 | + $localid = implode(",", $localid); |
| 24 | + } |
| 25 | + |
| 26 | + $params = array( |
| 27 | + "receptor" => $receptor, |
| 28 | + "sender" => $sender, |
| 29 | + "message" => $message, |
| 30 | + "date" => $date, |
| 31 | + "type" => $type, |
| 32 | + "localid" => $localid |
| 33 | + ); |
| 34 | + |
| 35 | + return $this->execute('sms/send.json', $params); |
| 36 | + } |
| 37 | + |
| 38 | + public function sendArray($receptor, $sender, $message, $date = null, $type = null, $localmessageid = null) |
| 39 | + { |
| 40 | + if (!is_array($receptor)) { |
| 41 | + $receptor = (array) $receptor; |
| 42 | + } |
| 43 | + if (!is_array($sender)) { |
| 44 | + $sender = (array) $sender; |
| 45 | + } |
| 46 | + if (!is_array($message)) { |
| 47 | + $message = (array) $message; |
| 48 | + } |
| 49 | + $repeat = count($receptor); |
| 50 | + if (!is_null($type) && !is_array($type)) { |
| 51 | + $type = array_fill(0, $repeat, $type); |
| 52 | + } |
| 53 | + if (!is_null($localmessageid) && !is_array($localmessageid)) { |
| 54 | + $localmessageid = array_fill(0, $repeat, $localmessageid); |
| 55 | + } |
| 56 | + |
| 57 | + $params = array( |
| 58 | + "receptor" => json_encode($receptor), |
| 59 | + "sender" => json_encode($sender), |
| 60 | + "message" => json_encode($message), |
| 61 | + "date" => $date, |
| 62 | + "type" => json_encode($type), |
| 63 | + "localmessageid" => json_encode($localmessageid) |
| 64 | + ); |
| 65 | + |
| 66 | + return $this->execute('sms/sendarray.json', $params); |
| 67 | + } |
| 68 | + |
| 69 | + public function status($messageid) |
| 70 | + { |
| 71 | + $params = array( |
| 72 | + "messageid" => is_array($messageid) ? implode(",", $messageid) : $messageid |
| 73 | + ); |
| 74 | + |
| 75 | + return $this->execute('sms/status.json',$params); |
| 76 | + } |
| 77 | + |
| 78 | + public function statusLocalMessageid($localid) |
| 79 | + { |
| 80 | + $params = array( |
| 81 | + "localid" => is_array($localid) ? implode(",", $localid) : $localid |
| 82 | + ); |
| 83 | + return $this->execute('sms/statuslocalmessageid.json', $params); |
| 84 | + } |
| 85 | + |
| 86 | + public function select($messageid) |
| 87 | + { |
| 88 | + $params = array( |
| 89 | + "messageid" => is_array($messageid) ? implode(",", $messageid) : $messageid |
| 90 | + ); |
| 91 | + |
| 92 | + return $this->execute('sms/select.json', $params); |
| 93 | + } |
| 94 | + |
| 95 | + public function selectOutbox($startdate, $enddate = null, $sender = null) |
| 96 | + { |
| 97 | + $params = array( |
| 98 | + "startdate" => $startdate, |
| 99 | + "enddate" => $enddate, |
| 100 | + "sender" => $sender |
| 101 | + ); |
| 102 | + |
| 103 | + return $this->execute('sms/selectoutbox.json', $params); |
| 104 | + } |
| 105 | + |
| 106 | + public function latestOutbox($pagesize = null, $sender = null) |
| 107 | + { |
| 108 | + $params = array( |
| 109 | + "pagesize" => $pagesize, |
| 110 | + "sender" => $sender |
| 111 | + ); |
| 112 | + |
| 113 | + return $this->execute('sms/latestoutbox.json', $params); |
| 114 | + } |
| 115 | + |
| 116 | + public function countOutbox($statustext, $startdate = null, $status = 1) |
| 117 | + { |
| 118 | + $params = array( |
| 119 | + "statustext" => $statustext, |
| 120 | + "startdate" => $startdate, |
| 121 | + "status" => $status |
| 122 | + ); |
| 123 | + |
| 124 | + return $this->execute('sms/countoutbox.json', $params); |
| 125 | + } |
| 126 | + |
| 127 | + public function cancel($messageid) |
| 128 | + { |
| 129 | + $params = array( |
| 130 | + "messageid" => is_array($messageid) ? implode(",", $messageid) : $messageid |
| 131 | + ); |
| 132 | + |
| 133 | + return $this->execute('sms/cancel.json',$params); |
| 134 | + } |
| 135 | + |
| 136 | + public function Receive($linenumber, $isread = 0) |
| 137 | + { |
| 138 | + $params = array( |
| 139 | + "linenumber" => $linenumber, |
| 140 | + "isread" => $isread |
| 141 | + ); |
| 142 | + |
| 143 | + return $this->execute('sms/receive.json', $params); |
| 144 | + } |
| 145 | + |
| 146 | + public function countInbox($startdate, $enddate, $linenumber, $isread = 0) |
| 147 | + { |
| 148 | + $params = array( |
| 149 | + "startdate" => $startdate, |
| 150 | + "enddate" => $enddate, |
| 151 | + "linenumber" => $linenumber, |
| 152 | + "isread" => $isread |
| 153 | + ); |
| 154 | + |
| 155 | + return $this->execute('sms/countinbox.json', $params); |
| 156 | + } |
| 157 | + |
| 158 | + public function countPostalcode($postalcode) |
| 159 | + { |
| 160 | + $params = array( |
| 161 | + "postalcode" => $postalcode |
| 162 | + ); |
| 163 | + return $this->execute('sms/countpostalcode.json', $params); |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + public function sendByPostalcode($postalcode, $sender, $message, $mcistartindex, $mcicount, $mtnstartindex, $mtncount, $date = null) |
| 168 | + { |
| 169 | + $params = array( |
| 170 | + "postalcode" => $postalcode, |
| 171 | + "sender" => $sender, |
| 172 | + "message" => $message, |
| 173 | + "mcistartindex" => $mcistartindex, |
| 174 | + "mcicount" => $mcicount, |
| 175 | + "mtnstartindex" => $mtnstartindex, |
| 176 | + "mtncount" => $mtncount, |
| 177 | + "date" => $date |
| 178 | + ); |
| 179 | + |
| 180 | + return $this->execute('sms/sendbypostalcode.json', $params); |
| 181 | + } |
| 182 | + |
| 183 | + public function lookup($receptor, $template, $token, $token2 = null, $token3 = null, $type = null) |
| 184 | + { |
| 185 | + $params = array( |
| 186 | + "receptor" => $receptor, |
| 187 | + "token" => $token, |
| 188 | + "token2" => $token2, |
| 189 | + "token3" => $token3, |
| 190 | + "template" => $template, |
| 191 | + "type" => $type |
| 192 | + ); |
| 193 | + |
| 194 | + return $this->execute('verify/lookup.json', $params); |
| 195 | + } |
| 196 | + |
| 197 | + private function execute($url, $params) |
| 198 | + { |
| 199 | + $client = new Client([ |
| 200 | + 'base_uri' => $this->base_url, |
| 201 | + ]); |
| 202 | + |
| 203 | + $response = $client->request('POST', $url, [ |
| 204 | + 'form_params' => $params |
| 205 | + ]); |
| 206 | + |
| 207 | + $body = (string)$response->getBody(); |
| 208 | + |
| 209 | +// return json_decode($body); |
| 210 | + return ($body); |
| 211 | + |
| 212 | +// return $this->base_url; |
| 213 | + } |
| 214 | +} |
0 commit comments