<?php
header("Content-type:text/html; charset=UTF-8");
/* *
* 類(lèi)名:YunxinSmsApi
* 功能:云信接口請(qǐng)求類(lèi)
* 詳細(xì):構(gòu)造云信短信接口請(qǐng)求,獲取遠(yuǎn)程HTTP數(shù)據(jù)
* 版本:1.3
* 日期:2018-05-12
* 說(shuō)明:
* 以下代碼只是為了方便客戶(hù)測(cè)試而提供的樣例代碼,客戶(hù)可以根據(jù)自己項(xiàng)目的需要,按照技術(shù)文檔自行編寫(xiě),并非一定要使用該代碼。
* 該代碼僅供學(xué)習(xí)和研究云信接口使用,只是提供一個(gè)參考。
*/
class YunxinSmsApi {
var $yunxin_config=array();
function __construct(){
//云信接口URL, 請(qǐng)求地址請(qǐng)參考云信互聯(lián)云通訊自助通平臺(tái)查看或者詢(xún)問(wèn)您的商務(wù)負(fù)責(zé)人獲取
$this->yunxin_config['api_send_url'] = 'https://u.smsyun.cc/sms-partner/access/{用戶(hù)帳號(hào)}/sendsms';
//云信賬號(hào) 替換成你自己的賬號(hào)
$this->yunxin_config['api_account']= '您的賬號(hào)';
//云信密碼 替換成你自己的密碼
$this->yunxin_config['api_password']= md5('您的密碼');
}
/**
* 發(fā)送短信
*
* @param string $mobile 手機(jī)號(hào)碼
* @param string $msg 短信內(nèi)容
* @param string $needstatus 是否需要狀態(tài)報(bào)告
*/
public function sendSMS( $mobile, $msg ) {
//云信接口參數(shù)
$postArr = array (
'smstype' =>'4',//短信發(fā)送發(fā)送
'clientid' => $this->yunxin_config['api_account'],
'password' => $this->yunxin_config['api_password'],
'mobile' => $mobile,
'content' => $msg ,
'sendtime'=>date('Y-m-d H:i:s'),
'extend'=>'00',
'uid'=>'00'
);
$result = $this->curlPost( $this->yunxin_config['api_send_url'] , $postArr);
return $result;
}
/**
* 通過(guò)CURL發(fā)送HTTP請(qǐng)求
* @param string $url //請(qǐng)求URL
* @param array $postFields //請(qǐng)求參數(shù)
* @return mixed
*/
private function curlPost($url,$postFields){
$postFields = json_encode($postFields);
echo $postFields.'
';
//echo $postFields;
$ch = curl_init ();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Accept-Encoding: identity',
'Content-Length: ' . strlen($postFields),
'Accept:application/json',
'Content-Type: application/json; charset=utf-8' //json版本需要填寫(xiě) Content-Type: application/json;
)
);
//curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //如果報(bào)錯(cuò) name lookup timed out 報(bào)錯(cuò)時(shí)添加這一行代碼
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt( $ch, CURLOPT_TIMEOUT,60);
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
$ret = curl_exec ( $ch );
if (false == $ret) {
$result = curl_error( $ch);
} else {
$rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
if (200 != $rsp) {
$result = "請(qǐng)求狀態(tài) ". $rsp . " " . curl_error($ch);
} else {
$result = $ret;
}
}
return $result;
}
}