function http_request($url, $data = null, $header = null, $method = 'GET') { //如果是Get传参,拼接字符串 if ($method == 'GET') { if (isset($data) && count($data) > 0) { $url.='?1=1'; foreach ($data as $key => $value) { $url.='&' . $key . '=' . $value; } $url = str_replace('?1=1&', '?', $url); } } $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'); //报文头设置 if (isset($header)) { curl_setopt($curl, CURLOPT_HTTPHEADER, $header); } //设置Post参数 if ($method == 'POST') { curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 if (isset($data)) { curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 } } //设置HTTPS curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //设置超时限制防止死循环 curl_setopt($curl, CURLOPT_TIMEOUT, 30); //显示返回的Header区域内容 curl_setopt($curl, CURLOPT_HEADER, 0); //获取的信息以文件流的形式返回 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //执行操作 $tmpInfo = curl_exec($curl); //关闭CURL会话 curl_close($curl); //返回数据 return $tmpInfo; }
function sendSms() { $phone = I('phone'); if (!empty($phone)) { $url = 'https://ca.aliyuncs.com/gw/alidayu/sendSms'; $data['rec_num'] =$phone; $data['sms_template_code'] = 'SMS_5013947'; $data['sms_free_sign_name'] = ''; $data['sms_type'] = 'normal'; $data['extend'] = '111111'; $data['sms_param'] = json_encode(array("code" => '' . NOW_TIME, "product" => "")); $header = array('X-Ca-Key:', 'X-Ca-Secret:'); echo $this->http_request($url, $data, $header); } }