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

阅读量:21
2021-04-18

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

1.GET

get.php

<?php
$host = '';
$port = 80;
$errno = '';
$errstr = '';
$timeout = 30;
$url = '/getapi.php';
$param = array(
    'name' => 'fdipzone',
    'gender' => 'man'
);
$url = $url.'?'.($param);
// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if(!$fp){
    return false;
}
// send request
$out = "GET ${url} rn";
$out .= "Host: ${host}rn";
$out .= "Connection:closernrn";
fputs($fp, $out);
// get response
$response = '';
while($row=fread($fp, 4096)){
    $response .= $row;
}
fclose($fp);
$pos = strpos($response, "rnrn");
$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 = '';
$port = 80;
$errno = '';
$errstr = '';
$timeout = 30;
$url = '/postapi.php';
$param = array(
    'name' => 'fdipzone',
    'gender' => 'man',
    'photo' => file_get_contents('photo.jpg')
);
$data = ($param);
// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if(!$fp){
    return false;
}
// send request
$out = "POST ${url} rn";
$out .= "Host:${host}rn";
$out .= "/x-rn";
$out .= "Content-length:".strlen($data)."rn";
$out .= "Connection:closernrn";
$out .= "${data}";
fputs($fp, $out);
// get response
$response = '';
while($row=fread($fp, 4096)){
    $response .= $row;
}
fclose($fp);
$pos = strpos($response, "rnrn");
$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="https://www.php.cn/'.$filename.'">';
?>

3.上传文件

file.php

<?php
$host = '';
$port = 80;
$errno = '';
$errstr = '';
$timeout = 30;
$url = '/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 = "--$boundaryrn";
// form data
foreach($form_data as $key=>$val){
    $data .= "Content-Disposition: form-data; name="".$key.""rn";
    $data .= "/plainrnrn";
    $data .= rawurlencode($val)."rn";
    $data .= "--$boundaryrn";
}
// file data
foreach($file_data as $file){
    $data .= "Content-Disposition: form-data; name="".$file['name'].""; filename="".$file['filename'].""rn";
    $data .= "Content-Type: ".mime_content_type($file['path'])."rnrn";
    $data .= implode("",file($file['path']))."rn";
    $data .= "--$boundaryrn";
}
$data .="--rnrn";
$out = "POST ${url} rn";
$out .= "Host:${host}rn";
$out .= "/form-data; boundary=$boundaryrn"; // /form-data
$out .= "Content-length:".strlen($data)."rn";
$out .= "Connection:closernrn";
$out .= "${data}";
fputs($fp, $out);
// get response
$response = '';
while($row=fread($fp, 4096)){
    $response .= $row;
}
fclose($fp);
$pos = strpos($response, "rnrn");
$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="https://www.php.cn/'.$filename.'">';
}
?>

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

先关推荐:

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

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

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

以上就是如何通过php 利用fsockopen /POST 提交表单及上传文件的详细内容,更多请关注星网无限其它相关文章!

声明:本文原创发布星网无限,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@处理

THE END

发表评论

相关推荐

  • 有关fgets()函数的文章推荐10篇

    以下正文:这篇文章主要介绍了PHP文件读写操作相关函数总结,本文总结了fwrite()、fread()、fgets()、fgetc()、file()、readf ...

    阅读量:65
    2021-04-19
  • php递归与无限分类实例详解

    这篇文章主要介绍了php实现递归与无限分类的方法,涉及php的递归操作技巧,需要的朋友可以参考下本文实例讲述了php实现递归与 ...

    阅读量:45
    2021-04-19
  • CI框架中zip类的使用

    CI框架自带的zip类简单实用,本文就来简单说一下ci框架的zip类的使用,需要的朋友可以参考下CI框架自带的zip类简单实用,本文 ...

    阅读量:44
    2021-04-18
  • SWFUpload插件上传文件的代码

    这篇文章主要介绍了文件上传之SWFUpload插件(代码),实现此代码主要分为两部分:1.前台文件和 2.后台文件upload.php,需要 ...

    阅读量:42
    2021-04-18
  • 关于destoon的URL Rewrite设置方法

    这篇文章主要介绍了destoon的URL Rewrite(伪静态)设置方法,需要的朋友可以参考下1、如果您的服务器支持.htaccess,则无需设 ...

    阅读量:47
    2021-04-18