php利用fsockopen GET/POST提交表单及上传文件
网络编程 2021-07-05 08:23www.168986.cn编程入门
这篇文章主要为大家详细介绍了php利用fsockopen GET/POST提交表单及上传文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
php利用fsockopen GET/POST提交表单及上传文件,具体内容如下
1.GET
get.php
<?php $host = 'demo.fdipzone.'; $port = 80; $errno = ''; $errstr = ''; $timeout = 30; $url = '/socket/getapi.php'; $param = array( 'name' => 'fdipzone', 'gender' => 'man' ); $url = $url.'?'.http_build_query($param); // create connect $fp = fsockopen($host, $port, $errno, $errstr, $timeout); if(!$fp){ return false; } // send request $out = "GET ${url} HTTP/1.1\r\n"; $out .= "Host: ${host}\r\n"; $out .= "Connection:close\r\n\r\n"; fputs($fp, $out); // get response $response = ''; while($row=fread($fp, 4096)){ $response .= $row; } fclose($fp); $pos = strpos($response, "\r\n\r\n"); $response = substr($response, $pos+4); echo $response; ?>
getapi.php
<?php $name = $_GET['name']; $gender = $_GET['gender']; echo 'name='.$name.'<br>'; echo 'gender='.$gender; ?>
2.POST
post.php
<?php $host = 'demo.fdipzone.'; $port = 80; $errno = ''; $errstr = ''; $timeout = 30; $url = '/socket/postapi.php'; $param = array( 'name' => 'fdipzone', 'gender' => 'man', 'photo' => file_get_contents('photo.jpg') ); $data = http_build_query($param); // create connect $fp = fsockopen($host, $port, $errno, $errstr, $timeout); if(!$fp){ return false; } // send request $out = "POST ${url} HTTP/1.1\r\n"; $out .= "Host:${host}\r\n"; $out .= "Content-type:application/x--form-urlencoded\r\n"; $out .= "Content-length:".strlen($data)."\r\n"; $out .= "Connection:close\r\n\r\n"; $out .= "${data}"; fputs($fp, $out); // get response $response = ''; while($row=fread($fp, 4096)){ $response .= $row; } fclose($fp); $pos = strpos($response, "\r\n\r\n"); $response = substr($response, $pos+4); echo $response; ?>
postapi.php
<?php define('UPLOAD_PATH', dirname(__FILE__).'/upload'); $name = $_POST['name']; $gender = $_POST['gender']; $photo = $_POST['photo']; $filename = time().'.jpg'; file_put_contents(UPLOAD_PATH.'/'.$filename, $photo, true); echo 'name='.$name.'<br>'; echo 'gender='.$gender.'<br>'; echo '<img src="upload/'.$filename.'">'; ?>
3.上传文件
file.php
<?php $host = 'demo.fdipzone.'; $port = 80; $errno = ''; $errstr = ''; $timeout = 30; $url = '/socket/fileapi.php'; $form_data = array( 'name' => 'fdipzone', 'gender' => 'man', ); $file_data = array( array( 'name' => 'photo', 'filename' => 'photo.jpg', 'path' =>'photo.jpg' ) ); // create connect $fp = fsockopen($host, $port, $errno, $errstr, $timeout); if(!$fp){ return false; } // send request srand((double)microtime()1000000); $boundary = "---------------------------".substr(md5(rand(0,32000)),0,10); $data = "--$boundary\r\n"; // form data foreach($form_data as $key=>$val){ $data .= "Content-Disposition: form-data; name=\"".$key."\"\r\n"; $data .= "Content-type:text/plain\r\n\r\n"; $data .= rawurlencode($val)."\r\n"; $data .= "--$boundary\r\n"; } // file data foreach($file_data as $file){ $data .= "Content-Disposition: form-data; name=\"".$file['name']."\"; filename=\"".$file['filename']."\"\r\n"; $data .= "Content-Type: ".mime_content_type($file['path'])."\r\n\r\n"; $data .= implode("",file($file['path']))."\r\n"; $data .= "--$boundary\r\n"; } $data .="--\r\n\r\n"; $out = "POST ${url} HTTP/1.1\r\n"; $out .= "Host:${host}\r\n"; $out .= "Content-type:multipart/form-data; boundary=$boundary\r\n"; // multipart/form-data $out .= "Content-length:".strlen($data)."\r\n"; $out .= "Connection:close\r\n\r\n"; $out .= "${data}"; fputs($fp, $out); // get response $response = ''; while($row=fread($fp, 4096)){ $response .= $row; } fclose($fp); $pos = strpos($response, "\r\n\r\n"); $response = substr($response, $pos+4); echo $response; ?>
fileapi.php
<?php define('UPLOAD_PATH', dirname(__FILE__).'/upload'); $name = $_POST['name']; $gender = $_POST['gender']; $filename = time().'.jpg'; echo 'name='.$name.'<br>'; echo 'gender='.$gender.'<br>'; if(move_uploaded_file($_FILES['photo']['tmp_name'], UPLOAD_PATH.'/'.$filename)){ echo '<img src="upload/'.$filename.'">'; } ?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程