Php Tianyi open platform SMS sending interface implementation method

Source: Internet
Author: User
This article describes how to implement the SMS sending interface of the php Tianyi open platform. it is a classic application of the SMS interface and is of great practical value, for more information about how to implement the SMS sending interface on the php Tianyi open platform, see the following example. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.