php 利用 fsockopen GET/POST 提交表单及上传文件
阅读原文时间:2023年07月08日阅读:1

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

getapi.php

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

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

postapi.php

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

3.上传文件

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

fileapi.php

'; echo 'gender='.$gender.'
'; if(move\_uploaded\_file($\_FILES\['photo'\]\['tmp\_name'\], UPLOAD\_PATH.'/'.$filename)){ echo ''; } ?>