首页  >  文章  >  后端开发  >  如何通过php 利用fsockopen GET/POST 提交表单及上传文件

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

jacklove
jacklove原创
2018-06-09 15:01:261622浏览

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;
?>

getapi.php

25eed5875dc39cb4ccbae3d67327971a';
echo 'gender='.$gender;
?>

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;
?>

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;;
?>

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;
?>

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;;
}
?>

本篇文章讲解了如何通过php 利用fsockopen GET/POST 提交表单及上传文件 ,更多相关内容请关注php中文网。

先关推荐:

关于mysql 优化 insert 性能 的相关介绍

如何使用php 常用自定义方法

如何通过php 使用异或(XOR)加密/解密文件

以上是如何通过php 利用fsockopen GET/POST 提交表单及上传文件的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn