URL
URL函数:
base64_encode(); //把字符串转换为64位,不可观的编码,加密
base64_decode(); //把字符串转换为可观的字符
urlencode(); //对URL字符串进行编码,把空格转换为加号
urldecode(); //对URL字符串进行解码,把空格转换为%20
1 <?php 2 $url="?username=张 三&password=123232"; 3 $base=base64_encode($url);//把字符串转换成64位,不可观的编码,加密 4 echo $base.'<br>'; 5 echo base64_decode($base);//转换为可观的字符 6 echo '<br>'; 7 echo urlencode($url);//把空格转换成加号 8 echo '<br>'; 9 echo urldecode($url); 10 echo '<br>'; 11 echo rawurlencode($url); 12 echo '<br>'; 13 echo rawurldecode($url); 14 echo '<br>'; 15 header("Content-type:text/html;charset=utf8"); 16 echo'<pre>'; 17 var_dump(get_headers("http://www.baidu.com"));//必须加http://才能使用哦 18 echo '<br>'; 19 echo'<pre>'; 20 var_dump(get_meta_tags("../d2-exam/Table2.html")); 21 echo '<br>'; 22 $arr=array( 23 'username'=>'张三', 24 'password'=>'123123', 25 'sex'=>'nv' 26 ); 27 $param= http_build_query($arr); 28 echo $param; 29 echo "<br>"; 30 var_dump($_GET); 31 echo '<br>'; 32 $url1="http://127.0.0.1/first_php/d/URL/url.php?user=%E4%B8%AD%E6%96%87"; 33 var_dump(parse_url($url1));//解析url 34 ?> 35 <a href="url.php?<?php echo $param;?>">点击</a>
fsockopen.php
fsockopen("www.baidu.com",80,$errno,$errstr,30); //用来模拟网站连接
1 <?php 2 header("Content-type:text/html;charset=utf-8"); 3 $fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);//$errno错误代码 $errno错误信息 4 //$data[1]=>'格式不正确' 30 超时时间,在这个时间段中进行请求 5 //端口号:服务器端口号默认80 网络连接必须是真是存在的,必须是线上(服务器上的),localhost是本地,线上是任何人都可以访问到的 6 //这个可以用来模拟网站登录 7 if (!$fp) { 8 echo "$errstr ($errno)<br />\n"; 9 } else { 10 $out = "GET / HTTP/1.1\r\n";//请求方式 11 $out .= "Host:www.baidu.com\r\n";//请求的主机名 12 $out .= "Connection: Close\r\n\r\n";//设置连接是否是持久连接alive(持久)或是close(短链接) 13 //$out .='User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 14 //Name'; //用户代理,可以随便改哪个浏览器, 15 fwrite($fp, $out); 16 while (!feof($fp)) { 17 echo fgets($fp, 124);//124是每次读取多少个字节 18 } 19 fclose($fp); 20 } 21 ?>
socket_post.php
模拟请求: 相当于是CURL
1 <?php 2 $url="http://127.0.0.1/first_php/d/URL/post.php";//处理post数据的路径 3 $arr= parse_url($url); 4 var_dump($arr); 5 $host=$arr['host']; 6 $port=empty($arr['port'])?80:$arr['port'];//因为port端口号为空,需要判断一下,下面设置默认80端口号 7 $path=$arr['path']; 8 $source=fsockopen($host, $port, $errno, $errstr, 30); 9 $data=['username'=>'张三','password'=>'111111']; 10 $data_str=http_build_query($data); 11 if(!$source){ 12 echo 'error:'.$errstr.$errno;//错误信息 错误代码 13 }else{//模拟请求,以下内容是固定格式 14 $con="POST $path HTTP/1.1\r\n"; 15 $con.="Host:$host\r\n"; 16 $con.="Content-type:application/x-www-form-urlencoded\r\n";//post请求默认编码格式application/x-www-form-urlencoded 17 $con.="Content-length:" . strlen($data_str)."\r\n"; 18 $con.="Connection:close\r\n\r\n";//设置连接为短链接,请求结束后连接断开 19 $con.="$data_str"; 20 fwrite($source, $con); 21 $header=true; 22 while (!feof($source)){//判断指针是否到结尾 23 if ($header){//加判断就不用输出头信息 24 $line=fgets($source); 25 if($line=="\r\n" || $line=="\n"){//单引号是字符串,双引号可以解析 26 $header=false; 27 } 28 }else{ 29 echo '<pre>'; 30 echo fgets($source); 31 } 32 } 33 }
post.php
1 <?php 2 var_dump($_POST); 3 4 //include 'html.html';
header函数:
header(“http/1.1 404 NOT Found”); //设置请求状态码
header("Content-type:text/html;charset=utf8"); //设置编码类型,文本类型是html,编码方式是utf8
application/octet-stream 代表任何类型
文件下载:
header("Content-type:image/jpeg"); //直接在页面显示而不可下载
header("Content-Disposition:attachment;filename=1234.jpg"); //设置文本类型为附件类型,filename文件名随意写,类型指定。
echo file_get_contents("../../resource/images/1.jpg"); //文件名不用和上边一样,类型一样即可
定位跳转功能:
header("Location:http://www.baidu.com"); //header其中一种用法,定位,跳转
headers_sent(); //检查HTTP头是否已发送
header_remove(); //移除之前设置的HTTP头
1 <?php 2 //header("http/1.1 404 NOT Found");//设置请求状态码 3 //header("Content-type:text/html;charset=utf8");//设置编码类型,文本类型是html,编码方式是utf8 4 //header('Content-type:application/octet-stream;charset=utf-8');//任意类型 5 ////header("Content-type:applation/txt;charset=utf8"); 6 //header('Content-Disposition:attachment;filename=1423.jpg');//设置文件类型为附件类型.filename文件名称随意,类型指定 7 //echo file_get_contents("../../resource/images/1.jpg");//文件名不用和上边设定的一样,类型一样即可 8 //echo "中国"; 9 //echo '<pre>'; 10 //var_dump(get_headers("http://www.baidu.com")); 11 //header("Location:http://www.baidu.com");//header其中一种用法,定位,跳转 12 13 14 header("HTTP/1.1 200 OK"); 15 //var_dump(get_headers("http://www.baidu.com")); 16 var_dump(headers_sent()); 17 //if(http_response_code()=='404'){ 18 // header('Location:http://www.baidu.com'); 19 //} 20 21 //echo ip2long('127.0.0.1');//把IP地址转换为数字字符串 22 //echo long2ip('65413464651646164');//把数字字符串转换为ip 23 24 //echo'<pre>'; 25 //var_dump(dns_get_record('www.baidu.com')); 26 //echo gethostbyaddr('127.0.0.1');//和下边ip一样 27 //echo gethostbyaddr('192.168.191.1'); 28 //echo gethostbyname('2013-20160307YL'); 29 //var_dump(gethostbynamel('2013-20160307YL')); 30 //echo '<br>'; 31 //echo getprotobyname('tcp'); 32 //echo getprotobynumber('6'); 33 ?> 34 <!--a连接可以直接下载文档(doc,pdf),如果是txt,jpg不会下载--> 35 <a href="1.txt">1.txt</a> 36 <a href="../../resource/images/1.jpg">1.jpg</a> 37 <a href="123.docx">1.doc</a>
浙公网安备 33010602011771号