TaobaoApiHelper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. /**
  5. * Class TaobaoApiHelper
  6. * @see https://api.alidayu.com/apitools/errorCodeSearch 错误码自查工具
  7. * @package common\helpers
  8. */
  9. class TaobaoApiHelper
  10. {
  11. public $serverUrl = 'http://gw.api.taobao.com/router/rest';
  12. public $appKey = '24485139';
  13. public $appSecret = 'd25ebf9219bdfdb3619750fc2ee40153';
  14. public function __construct($config = [])
  15. {
  16. foreach ($config as $k => $v) {
  17. $this->$k = $v;
  18. }
  19. }
  20. /**
  21. * 发送短信验证码
  22. *
  23. * 短信模板在控制台设置,当前的短信模板为
  24. * 【爱客】您的手机验证码为:9270,请妥善保管。感谢您对爱客的支持,老品牌,更放心。
  25. *
  26. * 接口返回值
  27. * {"result":{"err_code":"0","model":"219507513930394625^0","msg":"OK","success":true},"request_id":"10wc6gxcw1abj"}
  28. *
  29. * 当前设置每小时上限为5条,超过的话返回
  30. * {"code":15,"msg":"Remote service error","sub_code":"isv.BUSINESS_LIMIT_CONTROL","sub_msg":"\u89e6\u53d1\u5c0f\u65f6\u7ea7\u6d41\u63a7Permits:5","request_id":"ishbr0xnhhuk"}
  31. * 错误原因:触发业务流控限制
  32. * 解决方案:短信验证码,使用同一个签名,对同一个手机号码发送短信验证码,支持1条/分钟,5条/小时,10条/天。一个手机号码通过阿里大于平台只能收到40条/天。 短信通知,使用同一签名、同一模板,对同一手机号发送短信通知,允许每天50条(自然日)。
  33. *
  34. *
  35. * @param string $mobile
  36. * @param string $code
  37. * @return array
  38. * @see https://api.alidayu.com/doc2/apiDetail?apiId=25450 阿里大于API短信发送文档
  39. * @see https://dysms.console.aliyun.com/dysms.htm 控制台地址
  40. */
  41. public function sendSmsCode($mobile, $code)
  42. {
  43. require_once Yii::$app->getVendorPath() . '/taobao-sdk/TopSdk.php';
  44. $c = new \TopClient();
  45. $c->appkey = $this->appKey;
  46. $c->secretKey = $this->appSecret;
  47. $c->gatewayUrl = $this->serverUrl;
  48. $c->format = 'json';
  49. $c->checkRequest = false;
  50. $req = new \AlibabaAliqinFcSmsNumSendRequest();
  51. $req->setExtend("");
  52. $req->setSmsType("normal"); // 固定为normal
  53. $req->setSmsFreeSignName("爱客"); // 签名,即书名号里的文字
  54. $req->setSmsParam('{"number":"' . $code . '"}'); // 替换模板里的变量
  55. $req->setRecNum($mobile); // 手机号码
  56. $req->setSmsTemplateCode("SMS_73825029"); // 短信模板,在控制台申请
  57. $resp = $c->execute($req);
  58. $result = $this->getResult($resp);
  59. return $result;
  60. }
  61. /**
  62. * 处理结果
  63. * @param object $resp
  64. * @return array
  65. * 接口成功的返回值
  66. * {"result":{"err_code":"0","model":"219507513930394625^0","msg":"OK","success":true},"request_id":"10wc6gxcw1abj"}
  67. * 错误的返回值
  68. * {"code":15,"msg":"Remote service error","sub_code":"isv.BUSINESS_LIMIT_CONTROL","sub_msg":"\u89e6\u53d1\u5206\u949f\u7ea7\u6d41\u63a7Permits:1","request_id":"ej6jo9wcr62n"}
  69. */
  70. protected function getResult($resp)
  71. {
  72. $code = isset($resp->result->success) && $resp->result->success == true ? 1 : 0;
  73. $message = isset($resp->result->msg) ? $resp->result->msg : (isset($resp->sub_msg) ? $resp->sub_msg : '');
  74. if (!$code) {
  75. Yii::warning(json_encode($resp), __METHOD__);
  76. }
  77. return ['code' => $code, 'message' => $message];
  78. }
  79. }