<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace Plugin\sms\vendor\yunpian;

use \temmoku\db;

class put {
	public function put($mobile,$data=[],$code,$setting){
		$url='https://sms.yunpian.com/v2/sms/tpl_single_send.json';

		if(is_array($data)){
			foreach($data as $key=>$value){
				$_tpl_value[] =urlencode('#'.$key.'#').'='.urlencode($value);
			}
			$tpl_value=implode('&',$_tpl_value);
		}else{
			$tpl_value=urlencode('#code#').'='.urlencode($data);
		}

		$array=[

			'apikey' => $setting['apikey'],

            'mobile' => $mobile,

            'tpl_id'=>$setting['code'][$code],

            'tpl_value'=>$tpl_value

		];



		$data=$this->send($url,$array);

		if($data['code'] != 0){

			$data['text']=$data['msg'].",".$data['detail'];

		}

		return $data;

	}

	public function send($url,$data){

		$ch = curl_init();

		/* 设置验证方式 */

		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',

		    'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));

		/* 设置返回结果为流 */

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		/* 设置超时时间*/

		curl_setopt($ch, CURLOPT_TIMEOUT, 10);

		/* 设置通信方式 */

		curl_setopt($ch, CURLOPT_POST, 1);

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

	    curl_setopt ($ch, CURLOPT_URL, $url);

	    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

	    $result = curl_exec($ch);

	    $result = json_decode($result,true);

	    return $result;

	}

}

?>