本文實例講述了php天翼開放平台短信發送接口實現方法。分享給大家供大家參考。具體分析如下:
臨時性需求,研究了一下天翼開發平台的東西,用來發送驗證碼還是不錯的,但是每日限額不多,所以很雞肋,但是保證100%到達 買的話還是蠻貴的,代碼沒有做任何優化處理,只是測試是否可以實現接口,用的同學記得完善代碼,剛寫完老大又說是雞肋的東西,不用了,代碼放在博客記錄下,代碼如下:
復制代碼 代碼如下:<?php
//
date_default_timezone_set('PRC');
//獲取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);//使用post提交數據
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//設置 post提交的數據
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 從證書中檢查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$access_token = curl_exec($ch);
curl_close($ch);
$access_token = json_decode($access_token,true);
//獲取短信信任碼
$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']}×tamp=".$timestamp."&sign=".$sign);
$code = json_decode($code,true);
$code = $code['token'];
echo $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=http://wx.podapi.com/test.php&exp_time=2×tamp={$timestamp}&sign=".$sign;
$ch = curl_init("http://api.189.cn/v2/dm/randcode/send");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);//使用post提交數據
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//設置 post提交的數據
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 從證書中檢查SSL 加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$state = curl_exec($ch);
//var_dump(curl_getinfo($ch));
curl_close($ch);
//echo $state;
?>
希望本文所述對大家的php程序設計有所幫助。