接口调用以及memcache的使用

来源:互联网 发布:房子是用来住的 知乎 编辑:程序博客网 时间:2024/05/22 05:01

//接口调用

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>手机号码查询</title>
</head>
<body>
<center>
    <p>所要查询的手机号码:<br>
    <input type="text" name="" id="tel"><br><button>查询</button></p>
    <div class="box"></div>
</center>
    <script src="jq.js"></script>
    <script>
        $(document).on("click","button",function(){
            var tel = $("#tel").val();
            var str="<table border='1'><th>手机卡</th><th>归属地</th><th>城市</th><th>手机卡种类</th><th>手机卡类型</th>";
            if(isNaN(tel))
            {
                alert("您所要搜索的不是数字,请重新填写,谢谢");
            }
            else
            {
                if(!(/^[1][3578][0-9]{9}$/.test(tel)))
                {
                    alert("您所要搜索的手机号不正确,请重新填写,谢谢");
                }
                else
                {
                    $.ajax({
                        url:"select.php",
                        dataType:"json",
                        data:{tel:tel},
                        type:"post",
                        success:function(data){
                            var data=eval("("+data+")");
                            // // console.log(data.result)
                            str+="<tr>";
                            str+="<td>"+tel+"</td>",
                            str+="<td>"+data.result.province+"</td>",
                            str+="<td>"+data.result.city+"</td>",
                            str+="<td>"+data.result.company+"</td>",
                            str+="<td>"+data.result.card+"</td>",
                            str+="<tr></table>",
                            $(".box").html(str);
                        }
                    
                    })
                }
            }
        })
    </script>
</body>

</html>

//php  使用memcache

<?php
header("content-type:text/html;charset=utf-8");
$tel = $_POST['tel'];

$obj = new Memcache();
$obj->connect('127.0.0.1',11211) or die("链接失败!");
//根据接过来的手机号码先去查询memcache中的缓存,如果缓存中有的话,就调用缓存中的数据,如果没有,就调用接口
    $content = $obj->get("$tel");
    if($content)
    {
        echo json_encode($content);
    }else{
        $url="http://apis.juhe.cn/mobile/get?phone=".$tel."&key=77ed55394a03146546837671480e1acc";
        $content=file_get_contents($url);
        $obj -> set("$tel",$content);
        echo json_encode($content);
    }
?>

0 0
原创粉丝点击