最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

简述php使用fsockopen GET/POST进行表单提交和上传文件方法

来源:http://erdangjiade.com/topic/1090.html 你好,世界。 2017-09-25 22:42浏览(15)

这篇文章主要为大家详细介绍了php利用fsockopen GET/POST提交表单及上传文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

php利用fsockopen GET/POST提交表单及上传文件,具体内容如下

1.GET

get.php


<?php 
$host = 'demo.fdipzone.com'; 
$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
"; 
$out .= "Host: ${host}
"; 
$out .= "Connection:close

"; 
 
fputs($fp, $out); 
 
// get response 
$response = ''; 
while($row=fread($fp, 4096)){ 
  $response .= $row; 
} 
 
fclose($fp); 
 
$pos = strpos($response, "

"); 
$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.com'; 
$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
"; 
$out .= "Host:${host}
"; 
$out .= "Content-type:application/x-www-form-urlencoded
"; 
$out .= "Content-length:".strlen($data)."
"; 
$out .= "Connection:close

"; 
$out .= "${data}"; 
 
fputs($fp, $out); 
 
// get response 
$response = ''; 
while($row=fread($fp, 4096)){ 
  $response .= $row; 
} 
 
fclose($fp); 
 
$pos = strpos($response, "

"); 
$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.com'; 
$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
"; 
 
// form data 
foreach($form_data as $key=>$val){ 
  $data .= "Content-Disposition: form-data; name="".$key.""
"; 
  $data .= "Content-type:text/plain

"; 
  $data .= rawurlencode($val)."
"; 
  $data .= "--$boundary
"; 
} 
 
// file data 
foreach($file_data as $file){ 
  $data .= "Content-Disposition: form-data; name="".$file['name'].""; filename="".$file['filename'].""
"; 
  $data .= "Content-Type: ".mime_content_type($file['path'])."

"; 
  $data .= implode("",file($file['path']))."
"; 
  $data .= "--$boundary
"; 
} 
 
$data .="--

"; 
 
$out = "POST ${url} HTTP/1.1
"; 
$out .= "Host:${host}
"; 
$out .= "Content-type:multipart/form-data; boundary=$boundary
"; // multipart/form-data 
$out .= "Content-length:".strlen($data)."
"; 
$out .= "Connection:close

"; 
$out .= "${data}"; 
 
fputs($fp, $out); 
 
// get response 
$response = ''; 
while($row=fread($fp, 4096)){ 
  $response .= $row; 
} 
 
fclose($fp); 
 
$pos = strpos($response, "

"); 
$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.'">'; 
} 
?>

以上就是简述php利用fsockopen GET/POST进行表单提交和上传文件方法的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2