网易云短信PHP代码实现

$mobile];
		$res = $this -> _initRequest($url,$data);
		echo $res;
	}
	/**
	 * 验证码的校验成功返回200
	 * @param  string $mobile [description]
	 * @param  string $code   [description]
	 * @return [type]         [description]
	 */
	public function verifyCode($mobile = '',$code = '')
	{
		$url = "https://api.netease.im/sms/verifycode.action";
		$data = [
				'mobile'=>$mobile,
				'code' =>$code
			];
			$res = $this -> _initRequest($url,$data);
			echo $res;
	}
	/** 初始化头信息 */
	private function _initHeader()
	{
		date_default_timezone_set('Asia/Shanghai');
		$appKey = self::APP_KEY;
        $appSecret = self::APP_SECRET;
		$nonce = rand(10000,99999);
        $curTime = time();
        $checkSum = sha1($appSecret . $nonce . $curTime);
		$header = [
                    'Content-Type:application/x-www-form-urlencoded;charset=utf-8',
                    "AppKey:$appKey",
                    "Nonce:$nonce",
                    "CurTime:$curTime",
                    "CheckSum:$checkSum"
                ];
        return $header;
	}
	/**
	 * 利用curl发送
	 */
	private function _initRequest($url = '',$data = '')
	{
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, true);//post请求
		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));//http_build_query生成 URL-encode 之后的请求字符串
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_initHeader());
		$res = curl_exec($ch);
		curl_close($ch);
		return $res;
	}

}

简单的class实现,有啥一起讨论

转载于:https://my.oschina.net/u/3054299/blog/805102

你可能感兴趣的:(网易云短信PHP代码实现)