public static function canValidSms($mobile) { if (empty(self::getIp())) { data()->setErr('mobile', '无法确定你的位置。'); return false; } if (!Mobile::isMobile($mobile)) { data()->setErr('mobile', '不是有效的手机号。'); return false; } // 取得限定变量 $lmt = self::getLimitEnv($mobile); // 验证超限 if (self::isExcessValidTimes($mobile, $lmt)) { data()->setErr('mobile', '对不起,超过验证的安全次数,您可以重新获取验证码。'); return false; } return true; }
private static function getInfoForMobile($mobile, $type) { // 配置apiKey $apiKey = 'aeec72828c8a82f78b865e3c231afa31'; if (!empty(config(conFile() . 'apiKey.baiDu.mobile'))) { $apiKey = config(conFile() . 'apiKey.baiDu.mobile'); } if (!Mobile::isMobile($mobile)) { return null; } // 初始化一个cURL会话 $ch = curl_init(); $url = 'http://apis.baidu.com/apistore/mobilenumber/mobilenumber?phone=' . $mobile; $header = array('apikey: ' . $apiKey); // 添加apikey到header curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 需要获取的URL地址,也可以在curl_init()函数中设置。 curl_setopt($ch, CURLOPT_URL, $url); // 设置连接超时, 即服务器多少s无响应程序便会断开连接(官方说明:在发起连接前等待的时间,如果设置为0,则无限等待。) curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); // 设置连接成功后cURL接收数据最长时间 curl_setopt($ch, CURLOPT_TIMEOUT, 2); // 执行HTTP请求并将Json转换为对象 $rst = json_decode(curl_exec($ch)); // 出错处理 if (!empty($rst->errNum) || empty($rst)) { // 给网站管理员发送邮件 $err = '<h1>百度手机API接口没有成功返回地址信息!</h1>'; if (!empty($rst->errNum)) { $err .= '<h5>错误代号:' . $rst->errNum . '</h5>'; } if (!empty($rst->errMsg)) { $err .= '<h5>错误信息:' . $rst->errMsg . '</h5>'; } Email::sendEmailForWebmaster('【故障】百度手机归属地API出错', $err); return null; } return $type == 'city' ? $rst->retData->city : $rst->retData->province; }