欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php利用fsockopen GET/POST提交表单及上传文件

程序员文章站 2024-03-12 17:08:14
php利用fsockopen get/post提交表单及上传文件,具体内容如下 1.get get.php

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\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.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\r\n"; 
$out .= "host:${host}\r\n"; 
$out .= "content-type:application/x-www-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.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\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.'">'; 
} 
?> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。