PHP接口請求類php <?phphtml |
header("Content-type:text/html; charset=UTF-8");json |
/* *api |
* 類名:ChuanglanSmsApiapp |
* 功能:創藍接口請求類curl |
* 詳細:構造創藍短信接口請求,獲取遠程HTTP數據post |
* 版本:1.3學習 |
* 日期:2017-04-12測試 |
* 說明:網站 |
* 如下代碼只是爲了方便客戶測試而提供的樣例代碼,客戶能夠根據本身網站的須要,按照技術文檔自行編寫,並不是一 定要使用該代碼。 |
* 該代碼僅供學習和研究創藍接口使用,只是提供一個參考。 |
*/ |
class ChuanglanSmsApi { |
//Interface URL Used to send SMS |
const API_SEND_URL='http://intapi.253.com/send/json?'; |
//Interface URL Used to Query SMS balance |
const API_BALANCE_QUERY_URL='http://intapi.253.com/balance/json?'; |
const API_ACCOUNT='';//Get SMS Account from https://zz.253.com/site/login.html |
const API_PASSWORD='';//Get SMS Password from https://zz.253.com/site/login.html |
/** |
* 發送短信 |
* |
* @param string $mobile 手機號碼 |
* @param string $msg 短信內容 |
*/ |
public function sendInternational( $mobile, $msg) { |
//創藍接口參數 |
$postArr = array ( |
'account' => self::API_ACCOUNT, |
'password' => self::API_PASSWORD, |
'msg' => $msg, |
'mobile' => $mobile |
); |
$result = $this->curlPost( self::API_SEND_URL , $postArr); |
return $result; |
} |
/** |
* 查詢額度 |
* |
* 查詢地址 |
*/ |
public function queryBalance() { |
//查詢參數 |
$postArr = array ( |
'account' => self::API_ACCOUNT, |
'password' => self::API_PASSWORD, |
); |
$result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr); |
return $result; |
} |
/** |
* 經過CURL發送HTTP請求 |
* @param string $url //請求URL |
* @param array $postFields //請求參數 |
* @return mixed |
*/ |
private function curlPost($url,$postFields){ |
$postFields = json_encode($postFields); |
$ch = curl_init (); |
curl_setopt( $ch, CURLOPT_URL, $url ); |
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( |
'Content-Type: application/json; charset=utf-8' |
) |
); |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); |
curl_setopt( $ch, CURLOPT_POST, 1 ); |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields); |
curl_setopt( $ch, CURLOPT_TIMEOUT,1); |
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 = "請求狀態 ". $rsp . " " . curl_error($ch); |
} else { |
$result = $ret; |
} |
} |
curl_close ( $ch ); |
return $result; |
} |
} |