Home  >  Article  >  Backend Development  >  Send text messages in php (implemented by calling NetEase Cloud Letter)

Send text messages in php (implemented by calling NetEase Cloud Letter)

WBOY
WBOYOriginal
2016-07-28 08:28:521638browse

Call NetEase Cloud Letter to send text messages

	//网易云信发送短信
    //http://dev.netease.im/docs?doc=server&#发送模板短信
    function yx_sendsms($mobile='',$params='',$templateid=''){
        header("Content-Type:text/html; charset=utf-8");
        $AppKey = 'xxx';
        $AppSecret = 'xxx';
        $Nonce = rand(100000,999999);
        $CurTime = time();
        $CheckSum = strtolower(sha1($AppSecret.$Nonce.$CurTime));
        $url = 'https://api.netease.im/sms/sendtemplate.action';
        $head_arr = array();
        $head_arr[] = 'Content-Type: application/x-www-form-urlencoded';
        $head_arr[] = 'charset: utf-8';
        $head_arr[] = 'AppKey:'.$AppKey;
        $head_arr[] = 'Nonce:'.$Nonce;
        $head_arr[] = 'CurTime:'.$CurTime;
        $head_arr[] = 'CheckSum:'.$CheckSum;
        $data = array();
		$data['templateid'] = $templateid;
		$data['mobiles'] = $mobile;
		$data['params'] = $params;
		//var_dump($data);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $head_arr);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
		curl_setopt($ch, CURLOPT_TIMEOUT, 120);
		$result = curl_exec($ch);		
		curl_close($ch);
		$resArr = (array) json_decode($result);
        //$resArr = (array) json_decode('{"code":200,"msg":"sendid","obj":1}');
		//var_dump($resArr);
		//echo $resArr['code'];
		return $resArr;
    }

The above introduces PHP to send text messages (implemented by calling NetEase Yunxin), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn