<?php
header("Content-type:text/html; charset=UTF-8");
/* *
* 類名:YunxinSmsApi
* 功能:云信接口請求類
* 詳細:構(gòu)造云信短信接口請求,獲取遠程HTTP數(shù)據(jù)
* 版本:1.3
* 日期:2018-05-12
* 說明:
* 以下代碼只是為了方便客戶測試而提供的樣例代碼,客戶可以根據(jù)自己項目的需要,按照技術(shù)文檔自行編寫,并非一定要使用該代碼。
* 該代碼僅供學(xué)習(xí)和研究云信接口使用,只是提供一個參考。
*/
class YunxinSmsApi {
var $yunxin_config=array();
function __construct(){
//云信接口URL, 請求地址請參考云信互聯(lián)云通訊自助通平臺查看或者詢問您的商務(wù)負責人獲取
$this->yunxin_config['api_send_url'] = 'https://u.smsyun.cc/sms-partner/access/{用戶帳號}/sendsms';
//云信賬號 替換成你自己的賬號
$this->yunxin_config['api_account']= '您的賬號';
//云信密碼 替換成你自己的密碼
$this->yunxin_config['api_password']= md5('您的密碼');
}
/**
* 發(fā)送短信
*
* @param string $mobile 手機號碼
* @param string $msg 短信內(nèi)容
* @param string $needstatus 是否需要狀態(tài)報告
*/
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;
}
/**
* 通過CURL發(fā)送HTTP請求
* @param string $url //請求URL
* @param array $postFields //請求參數(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版本需要填寫 Content-Type: application/json;
)
);
//curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //如果報錯 name lookup timed out 報錯時添加這一行代碼
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 = "請求狀態(tài) ". $rsp . " " . curl_error($ch);
} else {
$result = $ret;
}
}
return $result;
}
}