How to use fsockopen GET/POST to submit forms and upload files through php

jacklove
Release: 2023-03-31 10:46:01
Original
1545 people have browsed it

php uses fsockopen GET/POST to submit forms and upload files

1.GET

get.php

 '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\r\n"; $out .= "Host: ${host}\r\n"; $out .= "Connection:close\r\n\r\n"; fputs($fp, $out); // get response $response = ''; while($row=fread($fp, 4096)){ $response .= $row; } fclose($fp); $pos = strpos($response, "\r\n\r\n"); $response = substr($response, $pos+4); echo $response; ?>
Copy after login

getapi.php

'; echo 'gender='.$gender; ?>
Copy after login

2.POST

post.php

 '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\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 = ''; while($row=fread($fp, 4096)){ $response .= $row; } fclose($fp); $pos = strpos($response, "\r\n\r\n"); $response = substr($response, $pos+4); echo $response; ?>
Copy after login

postapi.php

'; echo 'gender='.$gender.'
'; echo ''; ?>
Copy after login

3. Upload file

file .php

 '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\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['name']."\"; filename=\"".$file['filename']."\"\r\n"; $data .= "Content-Type: ".mime_content_type($file['path'])."\r\n\r\n"; $data .= implode("",file($file['path']))."\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 = ''; while($row=fread($fp, 4096)){ $response .= $row; } fclose($fp); $pos = strpos($response, "\r\n\r\n"); $response = substr($response, $pos+4); echo $response; ?>
Copy after login

fileapi.php

'; echo 'gender='.$gender.'
'; if(move_uploaded_file($_FILES['photo']['tmp_name'], UPLOAD_PATH.'/'.$filename)){ echo ''; } ?>
Copy after login

This article explains how to use fsockopen through php GET/POST submit forms and upload files. For more related content, please pay attention to the php Chinese website.

First recommendation:

Introduction to optimizing insert performance in mysql

##How to use PHP common custom methods

How to encrypt/decrypt files using XOR via php

The above is the detailed content of How to use fsockopen GET/POST to submit forms and upload files through php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!