php http请求
来源:互联网 发布:设计软件的模板 编辑:程序博客网 时间:2024/05/18 14:28
1: 用file_get_contents 以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的页面的具体地址,本例用的是fsock.php,fsock.php内容如下:
<?php echo "username:".$_POST['username']."<br/>"; echo "password:".$_POST['password']; ?>
结果为:
username:demopassword: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; }}?>
0 0
- php中Http请求!
- php请求http
- PHP模拟http请求
- PHP请求http(file_get_content)
- PHP 发送HTTP请求
- php 发送 http 请求
- php异步http请求
- php模拟http请求
- php http请求
- PHP模拟HTTP请求
- PHP之Http请求
- PHP模拟http请求
- php模拟http请求
- PHP HTTP请求签名
- PHP Laravel Http请求
- PHP 拼接HTTP请求字符串
- php模拟HTTP协议请求
- php http post get 请求
- bzoj3611: [Heoi2014]大工程
- Java数据结构和算法系列1--链表
- mysql查询语句in和exists二者的区别和性能影响
- Objective-C中的属性和实例变量
- 生成二维码
- php http请求
- Xcode中使用Git进行代码版本控制
- 第九章-----jsp标签
- 尺度不变特征变换匹配算法详解SIFT
- 设计模式C++实现--观察者模式
- Hibernate 参数设置一览表
- 线上引流压测工具Meteor
- #Objective-C 之 省市区 (数组-字典-数组-字典-数组)
- 常见的块状元素与内联元素