您的位置:首页 > 理论基础 > 计算机网络

php http请求

2015-12-23 15:07 627 查看
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的页
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;
}
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php url