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


    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.'">';
    }
    ?>

    源码下载地址:点击下载

  • 相关阅读:
    HashMap 统计一个字符串中每个单词出现的次数
    iOS .a静态库的制作及使用
    iOS framework静态库中使用xib和图片资源详解
    iOS 工程套子工程,主工程和framework工程或.a library静态库工程联调
    iOS 最新framework和.a静态库制作及使用全解(含工程套工程,多工程联调)
    iOS9新特性 3DTouch 开发教程全解(含源码)
    iOS GCD NSOperation NSThread等多线程各种举例详解
    Mac Beyond Compare 永久试用
    cocoapods 常见问题
    iOS 常用工具库LFKit功能介绍
  • 原文地址:https://www.cnblogs.com/fdipzone/p/3715088.html
Copyright © 2020-2023  润新知