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

墨辰丷
Lepaskan: 2023-03-27 12:04:01
asal
1677 orang telah melayarinya

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

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

1.GET

get.php

<?php 
$host = &#39;demo.fdipzone.com&#39;; 
$port = 80; 
$errno = &#39;&#39;; 
$errstr = &#39;&#39;; 
$timeout = 30; 
$url = &#39;/socket/getapi.php&#39;; 
 
$param = array( 
  &#39;name&#39; => &#39;fdipzone&#39;, 
  &#39;gender&#39; => &#39;man&#39; 
); 
 
$url = $url.&#39;?&#39;.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 = &#39;&#39;; 
while($row=fread($fp, 4096)){ 
  $response .= $row; 
} 
 
fclose($fp); 
 
$pos = strpos($response, "\r\n\r\n"); 
$response = substr($response, $pos+4); 
 
echo $response; 
?>
Salin selepas log masuk

getapi.php

<?php 
$name = $_GET[&#39;name&#39;]; 
$gender = $_GET[&#39;gender&#39;]; 
 
echo &#39;name=&#39;.$name.&#39;<br>&#39;; 
echo &#39;gender=&#39;.$gender; 
?>
Salin selepas log masuk

2.POST

post.php

<?php 
$host = &#39;demo.fdipzone.com&#39;; 
$port = 80; 
$errno = &#39;&#39;; 
$errstr = &#39;&#39;; 
$timeout = 30; 
$url = &#39;/socket/postapi.php&#39;; 
 
$param = array( 
  &#39;name&#39; => &#39;fdipzone&#39;, 
  &#39;gender&#39; => &#39;man&#39;, 
  &#39;photo&#39; => file_get_contents(&#39;photo.jpg&#39;) 
); 
 
$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 = &#39;&#39;; 
while($row=fread($fp, 4096)){ 
  $response .= $row; 
} 
 
fclose($fp); 
 
$pos = strpos($response, "\r\n\r\n"); 
$response = substr($response, $pos+4); 
 
echo $response; 
?>
Salin selepas log masuk

postapi.php

<?php 
define(&#39;UPLOAD_PATH&#39;, dirname(__FILE__).&#39;/upload&#39;); 
 
$name = $_POST[&#39;name&#39;]; 
$gender = $_POST[&#39;gender&#39;]; 
$photo = $_POST[&#39;photo&#39;]; 
 
$filename = time().&#39;.jpg&#39;; 
file_put_contents(UPLOAD_PATH.&#39;/&#39;.$filename, $photo, true); 
 
echo &#39;name=&#39;.$name.&#39;<br>&#39;; 
echo &#39;gender=&#39;.$gender.&#39;<br>&#39;; 
echo &#39;<img src="upload/&#39;.$filename.&#39;">&#39;; 
?>
Salin selepas log masuk

3.上传文件

file.php

<?php 
$host = &#39;demo.fdipzone.com&#39;; 
$port = 80; 
$errno = &#39;&#39;; 
$errstr = &#39;&#39;; 
$timeout = 30; 
$url = &#39;/socket/fileapi.php&#39;; 
 
$form_data = array( 
  &#39;name&#39; => &#39;fdipzone&#39;, 
  &#39;gender&#39; => &#39;man&#39;, 
); 
 
$file_data = array( 
  array( 
    &#39;name&#39; => &#39;photo&#39;, 
    &#39;filename&#39; => &#39;photo.jpg&#39;, 
    &#39;path&#39; =>&#39;photo.jpg&#39; 
  ) 
); 
 
// 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[&#39;name&#39;]."\"; filename=\"".$file[&#39;filename&#39;]."\"\r\n"; 
  $data .= "Content-Type: ".mime_content_type($file[&#39;path&#39;])."\r\n\r\n"; 
  $data .= implode("",file($file[&#39;path&#39;]))."\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 = &#39;&#39;; 
while($row=fread($fp, 4096)){ 
  $response .= $row; 
} 
 
fclose($fp); 
 
$pos = strpos($response, "\r\n\r\n"); 
$response = substr($response, $pos+4); 
 
echo $response; 
?>
Salin selepas log masuk

fileapi.php

<?php 
define(&#39;UPLOAD_PATH&#39;, dirname(__FILE__).&#39;/upload&#39;); 
 
$name = $_POST[&#39;name&#39;]; 
$gender = $_POST[&#39;gender&#39;]; 
 
$filename = time().&#39;.jpg&#39;; 
 
echo &#39;name=&#39;.$name.&#39;<br>&#39;; 
echo &#39;gender=&#39;.$gender.&#39;<br>&#39;; 
if(move_uploaded_file($_FILES[&#39;photo&#39;][&#39;tmp_name&#39;], UPLOAD_PATH.&#39;/&#39;.$filename)){ 
  echo &#39;<img src="upload/&#39;.$filename.&#39;">&#39;; 
} 
?>
Salin selepas log masuk

相关推荐:

php多线程php fsockopen解决办法

php之fsockopen()开放端口扫描器详解

fsockopen() 函数的开放端口扫描器

Atas ialah kandungan terperinci php利用fsockopen GET/POST提交表单及上传文件图文详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!