巅云智能建站平台搭建版(创业流派版)火爆上线,毕生受权!新增:文章智能收罗+全站真静态打包+都会分站+智能小法式+不法词过滤+H5自顺应+智能链词等功效功效概况
赞助文档Help

php操纵fsockopen GET/POST提交表单及上传文件

一佰互联网站建造(www.taishanly.com) 宣布日期 2020-04-25 14:41:33 阅读数: 115

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开辟和域名注册及假造主机办事履历,供给的办事更是天下着名。最近几年来还整合团队上风自立开辟了可视化多用户”“3.0平台版,拖拽排版网站建造设想,轻松完成pc站、手机微网站、小法式、APP一体化全网营销网站扶植 ,已胜利的为天下上百家收集公司供给自助建站平台搭建办事。

相干消息more

27
04月
php编程天天必学之考证码

本文为大师分享了php图片考证码的完成代码,分享给大师供大师参考,详细内容以下1.考证码图片天生captcha.php//1.天生100*3... >>概况

12
05月
专业网站扶植定制开辟的上风

专业网站扶植公司,首要觉得各至公司定制开辟网站为首要营业,那末定制开辟的上风究竟是甚么呢?定制开辟的公司网站与的通俗的网站带来的影响力有甚么... >>概况

11
04月
非科班福利!仅需5分钟,让你成为一位专业的画

编者按:非科班的画画喜好者来看!明天安利的这款App 相对是你的菜!这个操纵会一步步教你若何画好一小我。包含脸部、手部、半身、满身等详细部位... >>概况

08
04月
样样玩得来!UI标注神器Sketch Mea

@JingDesign :他开辟的Sketch插件为不计其数的APP界面设想师和网页设想师节流了有数可贵的时候,他开辟的插件架起了开辟工程师... >>概况

高端网站扶植

美工统筹SEO,为企业电子商务营销助力!

德律风:

023-85725751
建站

产物

域名注册 假造主机 云办事器 企业邮局
智能建站 APP打包 微站/小法式 创业平台
网站推行 媒体营销 智能收罗 AI机械人
400德律风 短信营销 店销机械人
私家定制 流派网站