PHP cURL学习-IP域名查询

大二学了一下PHP cURL,做了几个demo。

1、代码如下:

index.html

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1.0">
	<title>IP域名查询</title>
	<style type="text/css">
		*{margin: 0;padding: 0;border: 0;}
		body{font-family: 微软雅黑;width: 100%;background-color: #f8f8f8}
		@media (max-width: 450px){
			.ip_check_form{
				width: 100%;
				height: 500px;
				margin: 0 auto;
				background-color: ;
			}

		}
		@media (min-width: 450px){
			.ip_check_form{
				width: 100%;
				max-width: 600px;
				height: 800px;
				margin: 0 auto;
				background-color: #f8f8f8;
			}
		}
		form input{ 
			outline: none;
			font-family: 微软雅黑;
			font-size: 16px;
			transition: all .5s ease;
		}
		form input:focus {
			border-color: rgba(82, 168, 236, 0.8);
			outline: thin dotted \9;
			box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
		}
		p{padding: 10px 0 0 0;text-align: center;}
		.query_ip{
			width: 90%;
			height: 40px;
			padding-left: 5%;/*设置placeholder的位置*/
		}
		.submit{
			width: 90%;
			height: 50px;
			color: #fff;
			font-size: 16px;
			background-color: rgb(97,178,220);
			transition: all .5s ease;
			font-family: 微软雅黑;
		}
		.submit:hover{
			background-color: rgb(97,178,255);
		}
		
		/*input placeholder样式*/
		:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
		    opacity:.5; 
		}

		::-moz-placeholder { /* Mozilla Firefox 19+ */
		    opacity:.5;
		}

		input:-ms-input-placeholder{
		    opacity:.5;
		}

		input::-webkit-input-placeholder{
		    opacity:.5;
		}

		input::-webkit-input-placeholder {  /* WebKit browsers*/ 
			opacity:.5;
		}
		input:-moz-placeholder {  			/* Mozilla Firefox 4 to 18*/ 
			opacity:.5;
		}
		input::-moz-placeholder {  			/* Mozilla Firefox 19+*/ 
			opacity:.5;
		}
		input:-ms-input-placeholder { 		/* Internet Explorer 10+*/ 
			opacity:.5;
		}
	</style>
</head>
<body>
<form class="ip_check_form" method="get" action="query_ip.php">
	<p>
		<input class="query_ip" name="ip" type="text"/ required="" placeholder="127.0.0.1">
	</p>
	<p>
		<input class="submit" name="submit" type="submit" value="查询"/>
	</p>
	<p align="center" style="font-size: 12px;margin-top: 20px;">by Vegeta</p>
</form>

</body>
</html>

query_ip.php

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style type="text/css">
    *{margin: 0;padding: 0;border: 0;font-family: 微软雅黑;}
    body{background-color: #e9e9e9;}
    @media (min-width: 450px){
        .result_box{
            width: 100%;
            max-width: 600px;
            height: 800px;
            margin: 0 auto;
            background-color: #e9e9e9;
        }
    }
    .result_p0{
        width: 95%;
        height: 50px;
        display: block;
        padding-left: 5%;
        line-height: 50px;
        font-size: 14px;
        background-color: #e9e9e9;
        transition: background-color .5s ease; 
    }
    .result_p0:hover{
    	background-color: #fff;
    }
    .result_p1{
        width: 95%;
        height: 50px;
        display: block;
        padding-left: 5%;
        line-height: 50px;
        font-size: 14px;
        background-color: #f5f5f5;
        transition: background-color .5s ease; 
    }
    .result_p1:hover{
    	background-color: #fff;
    }
    .result_s{font-size: 12px;text-align: center;}
</style>
<?php
	header("Content-type:text/html,charset=utf8");
	include('simple_html_dom.php');
	$ip=$_GET['ip'];

	$url = 'http://ip.chinaz.com/'.$ip;

	$ch = curl_init();  
    for($i=0;$i<1;$i++)
    {
    	curl_setopt($ch, CURLOPT_URL, $url);  
    	curl_setopt($ch, CURLOPT_HEADER, 1);  
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 这个是主要参数  
        $output = curl_exec($ch) ;  
        $html = new simple_html_dom();
        $html ->load($output);
    	curl_close($ch);  
        $arrMsg = array();     
        
        foreach($html->find('.WhoIpWrap p span') as $m) {
            array_push($arrMsg,$m->plaintext);
        }
        // var_dump($arrMsg);

        if(!empty($arrMsg)) {
echo<<<begin
    <body>
        <div class="result_box">
            <br/>
            <p class="result_p1">$arrMsg[0]</p>
            <p class="result_p0">$arrMsg[4]</p>
            <p class="result_p1">$arrMsg[1]</p>
            <p class="result_p0">$arrMsg[5]</p>
            <p class="result_p1">$arrMsg[2]</p>
            <p class="result_p0">$arrMsg[6]</p>
            <p class="result_p1">$arrMsg[3]</p>
            <p class="result_p0">$arrMsg[7]</p>
            <br/><br/>
            <p class="result_s">以上数据来之站长之家</p>
        </div>
    </body>
begin;
            break;
        }
        else{
echo<<<begin
    <div class="result_box">
        <br/>
        <p align="center">抱歉,您输入IP域名不存在-_-!! 或者输入有误。</p>
    </div>
begin;
        break;
        }
    }


?>

simple_html_dom.php

需要下载php解析html类库:simple_html_dom.php

2、效果截图





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值