php 云片国外短信实例
<?php
namespace Ruifi\App;
class SmsL
{
public function smsIcode($moblie,$sms_code,$intl_code){
header("Content-Type:text/html;charset=utf-8");
$apikey = "";
$mobiles = "+".$intl_code.$moblie;
$text = "Shixi";
$smsArrL = array(
"apikey" => "$apikey",
"mobile" => "$mobiles",
"text" =>"$text"
);
$smsArrL_to = \Ruifi\Service\CommonService::gbk_to_utf8_arr($smsArrL); //编码转换
$apikey = $smsArrL_to['apikey'];
$mobilesm = $smsArrL_to['mobile'];
/* $mobiles = urlencode("$mobile");
$mobiles = urlencode("$mobiles");*/
//$text = $smsArrL_to['text'];
$ch = curl_init();
/* 设置验证方式 */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
/* 设置返回结果为流 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* 设置超时时间*/
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
/* 设置通信方式 */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 发送短信
/* $data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
$json_data = $this->send($ch,$data);
$array = json_decode($json_data,true);
return $array;*/
// 发送模板短信
// 需要对value进行编码
$data = array('tpl_id' => '2009674', 'tpl_value' => ('#code#').
'='.urlencode("$sms_code"), 'apikey' => $apikey, 'mobile' => $mobilesm);
$json_data = $this->tpl_send($ch,$data);
$array = json_decode($json_data,true);
return $array;
curl_close($ch);
}
/************************************************************************************/
function send($ch,$data){
curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
$this->checkErr($result,$error);
return $result;
}
function tpl_send($ch,$data){
curl_setopt ($ch, CURLOPT_URL,
'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
$this->checkErr($result,$error);
return $result;
}
function checkErr($result,$error) {
if($result === false)
{
echo 'Curl error: ' . $error;
}
else
{
//echo '操作完成没有任何错误';
}
}
}
调用:
//传递手机号 验证码 和 国家区号
$SmssL = new \Ruifi\App\SmsL(); $Code = $SmssL->smsIcode("$mobile","$sms_code","$intl_code"); if ($Code['code'] == '0'){ $codeState = 'OK'; }

浙公网安备 33010602011771号