Maison > Article > développement back-end > Comment utiliser fsockopen GET/POST pour soumettre des formulaires et télécharger des fichiers via php
php utilise fsockopen GET/POST pour soumettre des formulaires et télécharger des fichiers
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\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; ?>
getapi.php
25eed5875dc39cb4ccbae3d67327971a'; echo 'gender='.$gender; ?>
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\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; ?>
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.'">'; ?>
fileapi.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\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; ?>
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.'">'; } ?>
Cet article explique comment utiliser fsockopen GET/POST pour soumettre des formulaires et télécharger des fichiers via PHP. Pour plus de contenu connexe, veuillez prêter attention au site Web PHP chinois.
Recommandé en premier :
Introduction connexe à l'optimisation MySQL des performances d'insertion
Comment utiliser les méthodes de personnalisation courantes de PHP
Comment crypter/déchiffrer des fichiers en utilisant le OU exclusif (XOR) via php
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!