php http请求
2015-12-23 15:07
627 查看
1: 用file_get_contents 以get方式获取内容:
如需设置超时时间,需添加条件配置,如下设置0.01秒超时
POST方式
2: 用fopen打开url, 以get方式获取内容:
3: curl:
下面是上传本地文件的代码
3: fsockopen:
简要说明:代码第二行是你的IP地址或域名,第四行是你要POST的页
c374
面的具体地址,本例用的是fsock.php,fsock.php内容如下:
结果为:
伪造post,get方法
<?php $url='http://www.baidu.com'; $html = file_get_contents($url); echo $html; ?>
如需设置超时时间,需添加条件配置,如下设置0.01秒超时
$url='http://www.baidu.com'; $opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>0.01, ) ); $context = stream_context_create($opts); $html = file_get_contents($url,false,$context); echo $html
POST方式
<?php $data = array ('foo' => 'bar'); $data = http_build_query($data); $opts = array ( 'http' => array ( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencodedrn Content-Length: '.strlen($data).'rn', 'content' => $data, 'timeout' => 5 ) ); $context = stream_context_create($opts); $html = file_get_contents('http://www.baidu.com', false, $context); echo $html;
2: 用fopen打开url, 以get方式获取内容:
<?php $url='http://www.baidu.com'; $fp = fopen($url,'r'); stream_get_meta_data($fp); while(!feof($fp)){ $result.= fgets($fp, 1024); } echo "url body: $result"; fclose($fp);
3: curl:
$url = ''; $data = array('a'=> 'b'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $ret = curl_exec($ch); curl_close($ch); 主要说下这个选项CURLOPT_RETURNTRANSFER:如果设置为true/1,则curl_exec的时候不会自动将请求网页的内容输出到屏幕,$ret为请求网页的内容,如果设置为false/0,则curl_exec的时候会自动将请求网页的内容输出到屏幕,此时如果请求成功的话$ret的内容是1或者true。
下面是上传本地文件的代码
$url = ''; $file = '1.jpg'; $field['uploadFile'] = '@'.$file;(uploadFile为接收端的name名) $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $field); $ret = curl_exec($ch); curl_close($ch);
3: fsockopen:
$uploadInfo = array( 'host'=>'', 'port'=>'80', 'url'=>'/upload.php' ); $fp = fsockopen($uploadInfo['host'],$uploadInfo['port'],$errno,$errstr); $file = '1.jpg'; $content = file_get_contents($file); $boundary = md5(time()); out.="--".$boundary."\r\n"; $out.="Content-Disposition: form-data; name=\"uploadFile\"; filename=\"".$file."\"\r\n"; $out.="Content-Type: image/jpg\r\n\r\n"; $out.=$content."\r\n"; $out.="--".$boundary."\r\n"; fwrite($fp,"POST ".$uploadInfo['url']." HTTP/1.1\r\n"); fwrite($fp,"Host:".$uploadInfo['host']."\r\n"); fwrite($fp,"Content-Type: multipart/form-data; boundary=".$boundary."\r\n"); fwrite($fp,"Content-length:".strlen($out)."\r\n\r\n"); fwrite($fp,$out); while (!feof($fp)){ $ret .= fgets($fp, 1024); } fclose($fp); $ret = trim(strstr($ret, "\r\n\r\n")); preg_match('/http:.*/', $ret, $match); return $match[0];
简要说明:代码第二行是你的IP地址或域名,第四行是你要POST的页
c374
面的具体地址,本例用的是fsock.php,fsock.php内容如下:
<?php echo "username:".$_POST['username']."<br/>"; echo "password:".$_POST['password']; ?>
结果为:
username:demo password:hahaha
伪造post,get方法
<?php //fsocket模拟post提交 $purl = "http://localhost/netphp/test2.php?uu=rrrrrrrrrrrr"; print_r(parse_url($url)); sock_post($purl,"uu=55555555555555555"); //fsocket模拟get提交 function sock_get($url, $query) { $info = parse_url($url); $fp = fsockopen($info["host"], 80, $errno, $errstr, 3); $head = "GET ".$info['path']."?".$info["query"]." HTTP/1.0rn"; $head .= "Host: ".$info['host']."rn"; $head .= "rn"; $write = fputs($fp, $head); while (!feof($fp)) { $line = fread($fp,4096); echo $line; } } sock_post($purl,"uu=rrrrrrrrrrrrrrrr"); function sock_post($url, $query) { $info = parse_url($url); $fp = fsockopen($info["host"], 80, $errno, $errstr, 3); $head = "POST ".$info['path']."?".$info["query"]." HTTP/1.0rn"; $head .= "Host: ".$info['host']."rn"; $head .= "Referer: http://".$info['host'].$info['path']."rn"; $head .= "Content-type: application/x-www-form-urlencodedrn"; $head .= "Content-Length: ".strlen(trim($query))."rn"; $head .= "rn"; $head .= trim($query); $write = fputs($fp, $head); while (!feof($fp)) { $line = fread($fp,4096); echo $line; } } ?>
相关文章推荐
- 一个关于if else容易迷惑的问题
- java-用HttpURLConnection发送Http请求.
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- VBScript 剪贴板抓取URL并在浏览器中打开
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- 用vbs 实现从剪贴板中抓取一个 URL 然后在浏览器中打开该 Web 站点
- asp获取URL参数的几种方法分析总结[原创]_应用技巧_脚本之家