This article mainly introduces the implementation of the SMS sending interface on the php Tianyi open platform. It is a classic application of the SMS interface and is very useful. For more information, see
This article mainly introduces the implementation of the SMS sending interface on the php Tianyi open platform. It is a classic application of the SMS interface and is very useful. For more information, see
This article describes how to implement the SMS sending interface of the php Tianyi open platform. Share it with you for your reference. The specific analysis is as follows:
Due to temporary requirements, I studied things on the Tianyi development platform. It is good to send verification codes, but the daily quota is not much, so it is very bad, but it is quite expensive to guarantee that 100% of the requests will arrive at the purchase, the code is not optimized, but is used to test whether the interface can be implemented. The user remembers to improve the Code. After writing the code, the boss said it was a chicken fault. No, the code is recorded on the blog, the Code is as follows:
The Code is as follows:
<? Php
//
Date_default_timezone_set ('prc ');
// Obtain access_token
$ Data = "app_id = x & app_secret = x & grant_type = client_credentials ";
$ Ch = curl_init ("https://oauth.api.189.cn/emp/oauth2/v2/access_token ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_POST, 1); // submit data using post
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // sets the data submitted by post.
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
// Check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
$ Access_token = curl_exec ($ ch );
Curl_close ($ ch );
$ Access_token = json_decode ($ access_token, true );
// Obtain the SMS trust code
$ Timestamp = date ('Y-m-d H: I: s ');
$ Param ['app _ id'] = "app_id = x ";
$ Param ['Access _ token'] = "access_token =". $ access_token ['Access _ token'];
$ Param ['timestamp'] = "timestamp =". $ timestamp;
Ksort ($ param );
$ Plaintext = implode ("&", $ param );
$ Sign = rawurlencode (base64_encode (hash_hmac ('sha1', $ plaintext, app_secret, true )));
// Echo $ sign; exit;
$ Code = file_get_contents ("http://api.189.cn/v2/dm/randcode/token? App_id = x & access_token = {$ access_token ['Access _ token']} & timestamp = ". $ timestamp." & sign = ". $ sign );
$ Code = json_decode ($ code, true );
$ Code = $ code ['Token'];
Echo $ code;
// Send the verification code
Unset ($ param, $ plaintext, $ sign );
$ Param ['app _ id'] = "app_id = x ";
$ Param ['Access _ token'] = "access_token =". $ access_token ['Access _ token'];
$ Param ['Token'] = "token =". $ code;
$ Param ['phone'] = "phone = 15091421612 ";
$ Param ['url'] = "url = http://wx.podapi.com/test.php ";
$ Param ['exp _ time'] = "exp_time = 2 ";
$ Param ['timestamp'] = "timestamp =". $ timestamp;
Ksort ($ param );
$ Plaintext = implode ("&", $ param );
$ Sign = rawurlencode (base64_encode (hash_hmac ('sha1', $ plaintext, 'XX', true )));
$ Data = "app_id = x & access_token = {$ access_token ['Access _ token']} & token = {$ code} & phone = 15091421612 & url = token ". $ sign;
$ Ch = curl_init ("http://api.189.cn/v2/dm/randcode/send ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_POST, 1); // submit data using post
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // sets the data submitted by post.
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
// Check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
$ State = curl_exec ($ ch );
// Var_dump (curl_getinfo ($ ch ));
Curl_close ($ ch );
// Echo $ state;
?>
I hope this article will help you with php programming.
,