smsfire / php-sdk
适用于所有 SMSFIRE 服务的 SDK
1.3.0
2021-11-22 19:40 UTC
Requires
- php: >=7.1.0
Requires (Dev)
README
需求
- Composer > 2+
- PHP >= 7.1.0
- 用户名和密码 (在此注册)
安装
您可以通过 composer 或下载源代码来安装 php-sdk
Composer
php-sdk 作为 smsfire/php-sdk 包在 Packagist 上提供
composer require smsfire/php-sdk
SMS 服务
此服务的参考可以在 此处 找到
命名空间 - Smsfire\Sms\Messages
此命名空间允许您发送短信消息。
- sendIndividual() - 发送单个短信消息
- sendBulk() - 发送批量短信消息
接收消息 (MO)
当您在 sendIndividual() 或 sendBulk() 消息方法中将
allowReply
参数设置为 true 时,您的账户可能会因每条接收到的消息产生额外的费用。请联系您的账户经理了解详细信息。闪光短信 - 类 0
flash
参数取决于您账户中设置的路线以及每个运营商对此功能的可用性。请联系您的账户经理了解详细信息。
发送单个消息 - sendIndividual()
访问 参考文档 检查数据响应以及此方法每个参数的详细信息。
此方法上可用的参数指南
示例
//Load composer autoload file require './vendor/autoload.php'; use Smsfire\Sms\Messages; use Smsfire\Exceptions\SmsfireException; use Smsfire\Exceptions\HttpException; try { $user = 'myuser'; //Same user that is used to access Dashboard $pass = 'mypass'; //Same password that is used to access Dashboard $token = base64_encode("{$user}:{$pass}"); /** * Pass base64 token on Message instance * Check guide table of params */ $messagesService = new Messages($token); $response = $messagesService->sendIndividual( $to, $text, $from, $customId, $campaignId, $flash, $allowReply, $scheduleTime, $debug ); /** * Response as raw text * Good to use when Debug option is true */ echo $response; //Response with the statusCode of Http response echo $response->statusCode(); //Response as json string echo $response->__toJson(); //Response as object print_r($response->__toObject()); //Response as array print_r($response->__toArray()); } catch (SmsfireException $e) { echo $e->getMessage(); } catch (HttpException $e) { echo $e->getMessage(); } catch (Exception $e) { echo $e->getMessage(); }
发送批量消息 - sendBulk()
访问 参考文档 检查数据响应以及此方法每个参数的详细信息。
此方法上可用的参数指南
示例
//Load composer autoload file require './vendor/autoload.php'; use Smsfire\Sms\Messages; use Smsfire\Exceptions\SmsfireException; use Smsfire\Exceptions\HttpException; try { $user = 'myuser'; //Same user that is used to access Dashboard $pass = 'mypass'; //Same password that is used to access Dashboard $token = base64_encode("{$user}:{$pass}"); //Pass base64 token on Message instance $messagesService = new Messages($token); //Minimum of two items to use Bulk request $destinations = [ [ 'to' => '5511944556677', 'text' => 'My first message', 'customId' => 'abc-00001', 'flash' => true ], [ 'to' => '5565988887777', 'text' => 'My second message' ] ]; $response = $messagesService->sendBulk( $destinations, $campaignId, $allowReply, $scheduleTime, $debug ); /** * Response as raw text * Good to use when Debug option is true */ echo $response; //Response with the statusCode of Http response echo $response->statusCode(); //Response as json string echo $response->__toJson(); //Response as object print_r($response->__toObject()); //Response as array print_r($response->__toArray()); } catch (SmsfireException $e) { echo $e->getMessage(); } catch (HttpException $e) { echo $e->getMessage(); } catch (Exception $e) { echo $e->getMessage(); }
命名空间 - Smsfire\Sms\Inbox
此命名空间允许您获取短信收件箱中的收件消息。
- getAll() - 获取已读和未读消息
- getUnread() - 获取未读消息
访问 参考文档 检查数据响应以及此方法每个参数的详细信息。
当您的收件箱没有消息时,将返回 REF. 204 No Content 状态码 204。
获取所有消息 - getAll()
示例
//Load composer autoload file require './vendor/autoload.php'; use Smsfire\Sms\Inbox; use Smsfire\Exceptions\SmsfireException; use Smsfire\Exceptions\HttpException; try { $debug = false; //Debug API request - DEFAULT: false $user = 'myuser'; //Same user that is used to access Dashboard $pass = 'mypass'; //Same password that is used to access Dashboard $token = base64_encode("{$user}:{$pass}"); /** * Pass base64 token on Inbox instance * Check guide table of params */ $inboxServices = new Inbox($token); $response = $inboxServices->getAll($debug); /** * Response as raw text * Good to use when Debug option is true */ echo $response; //Response with the statusCode of Http response echo $response->statusCode(); //Response as json string echo $response->__toJson(); //Response as object print_r($response->__toObject()); //Response as array print_r($response->__toArray()); //Handle empty inbox if($response->statusCode() === 204) { echo "Empty inbox"; } } catch (SmsfireException $e) { echo $e->getMessage(); } catch (HttpException $e) { echo $e->getMessage(); } catch (Exception $e) { echo $e->getMessage(); }
由于 API 限制,此方法将公开您收件箱中 最后 100 条接收到的消息。更多内容,请访问 SMSFire 门户 并在菜单 SMS > 收件箱中访问。
获取未读消息 - getUnread()
示例
//Load composer autoload file require './vendor/autoload.php'; use Smsfire\Sms\Inbox; use Smsfire\Exceptions\SmsfireException; use Smsfire\Exceptions\HttpException; try { $debug = false; //Debug API request - DEFAULT: false $user = 'myuser'; //Same user that is used to access Dashboard $pass = 'mypass'; //Same password that is used to access Dashboard $token = base64_encode("{$user}:{$pass}"); /** * Pass base64 token on Inbox instance * Check guide table of params */ $inboxServices = new Inbox($token); $response = $inboxServices->getUnread($debug); /** * Response as raw text * Good to use when Debug option is true */ echo $response; //Response with the statusCode of Http response echo $response->statusCode(); //Response as json string echo $response->__toJson(); //Response as object print_r($response->__toObject()); //Response as array print_r($response->__toArray()); //Handle empty inbox if($response->statusCode() === 204) { echo "Empty inbox"; } } catch (SmsfireException $e) { echo $e->getMessage(); } catch (HttpException $e) { echo $e->getMessage(); } catch (Exception $e) { echo $e->getMessage(); }
命名空间 - Smsfire\Sms\Status
此命名空间允许您通过 id
或 customId
获取消息状态
- messageIds() - 通过 id 获取消息状态
- customIds() - 通过 customId 获取消息状态
访问 参考文档,以检查数据响应和此方法每个参数的详细信息。
当提供的 id 或 customId 不存在时,将返回 statusCode 204。 参考 204 No Content
通过 id 获取消息状态 - messageIds()
此方法上可用的参数指南
示例
//Load composer autoload file require './vendor/autoload.php'; use Smsfire\Sms\Status; use Smsfire\Exceptions\SmsfireException; use Smsfire\Exceptions\HttpException; try { $user = 'myuser'; //Same user that is used to access Dashboard $pass = 'mypass'; //Same password that is used to access Dashboard $token = base64_encode("{$user}:{$pass}"); /** * Pass base64 token on Status instance * Check guide table of params */ $messagesStatus = new Status($token); $ids = "000001,000002,000003"; //Messages ids - Comma separated $response = $messagesStatus->messageIds($ids, $debug); /** * Response as raw text * Good to use when Debug option is true */ echo $response; //Response with the statusCode of Http response echo $response->statusCode(); //Response as json string echo $response->__toJson(); //Response as object print_r($response->__toObject()); //Response as array print_r($response->__toArray()); //Handle empty result if($response->statusCode() === 204) { echo "Message id does not exist"; } } catch (SmsfireException $e) { echo $e->getMessage(); } catch (HttpException $e) { echo $e->getMessage(); } catch (Exception $e) { echo $e->getMessage(); }
由于 API 限制,此方法最多接受 200 个消息 id。更多内容,请访问 SMSFire 门户 并在菜单 SMS > 报告中访问。
通过 customId 获取消息状态 - customIds()
此方法上可用的参数指南
示例
//Load composer autoload file require './vendor/autoload.php'; use Smsfire\Sms\Status; use Smsfire\Exceptions\SmsfireException; use Smsfire\Exceptions\HttpException; try { $user = 'myuser'; //Same user that is used to access Dashboard $pass = 'mypass'; //Same password that is used to access Dashboard $token = base64_encode("{$user}:{$pass}"); /** * Pass base64 token on Status instance * Check guide table of params */ $messagesStatus = new Status($token); $customIds = "myid0001,myid000002,myid000003"; //Custom ids - Comma separated $response = $messagesStatus->customIds($customIds, $debug); /** * Response as raw text * Good to use when Debug option is true */ echo $response; //Response with the statusCode of Http response echo $response->statusCode(); //Response as json string echo $response->__toJson(); //Response as object print_r($response->__toObject()); //Response as array print_r($response->__toArray()); //Handle empty result if($response->statusCode() === 204) { echo "Custom id does not exist"; } } catch (SmsfireException $e) { echo $e->getMessage(); } catch (HttpException $e) { echo $e->getMessage(); } catch (Exception $e) { echo $e->getMessage(); }
命名空间 - Smsfire\Exceptions
自定义异常,可让您更好地处理错误。
SmsfireException
当任何 SDK 所需的类型和数据不符合要求时,将抛出此异常。
HttpException
当核心 API 存在请求问题,如超时或数据错误等时,将抛出此异常。