查看: 2418|回复: 0

[PHP学习] php利用fsockopen GET/POST提交表单及上传文件

发表于 2018-4-1 10:54:17

php利用fsockopen GET/POST提交表单及上传文件,具体内容如下

1.GET

get.php

  1. <?php
  2. $host = 'demo.fdipzone.com';
  3. $port = 80;
  4. $errno = '';
  5. $errstr = '';
  6. $timeout = 30;
  7. $url = '/socket/getapi.php';
  8. $param = array(
  9. 'name' => 'fdipzone',
  10. 'gender' => 'man'
  11. );
  12. $url = $url.'?'.http_build_query($param);
  13. // create connect
  14. $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  15. if(!$fp){
  16. return false;
  17. }
  18. // send request
  19. $out = "GET ${url} HTTP/1.1\r\n";
  20. $out .= "Host: ${host}\r\n";
  21. $out .= "Connection:close\r\n\r\n";
  22. fputs($fp, $out);
  23. // get response
  24. $response = '';
  25. while($row=fread($fp, 4096)){
  26. $response .= $row;
  27. }
  28. fclose($fp);
  29. $pos = strpos($response, "\r\n\r\n");
  30. $response = substr($response, $pos+4);
  31. echo $response;
  32. ?>
复制代码

getapi.php

  1. <?php
  2. $name = $_GET['name'];
  3. $gender = $_GET['gender'];
  4. echo 'name='.$name.'<br>';
  5. echo 'gender='.$gender;
  6. ?>
复制代码

2.POST

post.php

  1. <?php
  2. $host = 'demo.fdipzone.com';
  3. $port = 80;
  4. $errno = '';
  5. $errstr = '';
  6. $timeout = 30;
  7. $url = '/socket/postapi.php';
  8. $param = array(
  9. 'name' => 'fdipzone',
  10. 'gender' => 'man',
  11. 'photo' => file_get_contents('photo.jpg')
  12. );
  13. $data = http_build_query($param);
  14. // create connect
  15. $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  16. if(!$fp){
  17. return false;
  18. }
  19. // send request
  20. $out = "POST ${url} HTTP/1.1\r\n";
  21. $out .= "Host:${host}\r\n";
  22. $out .= "Content-type:application/x-www-form-urlencoded\r\n";
  23. $out .= "Content-length:".strlen($data)."\r\n";
  24. $out .= "Connection:close\r\n\r\n";
  25. $out .= "${data}";
  26. fputs($fp, $out);
  27. // get response
  28. $response = '';
  29. while($row=fread($fp, 4096)){
  30. $response .= $row;
  31. }
  32. fclose($fp);
  33. $pos = strpos($response, "\r\n\r\n");
  34. $response = substr($response, $pos+4);
  35. echo $response;
  36. ?>
复制代码

postapi.php

  1. <?php
  2. define('UPLOAD_PATH', dirname(__FILE__).'/upload');
  3. $name = $_POST['name'];
  4. $gender = $_POST['gender'];
  5. $photo = $_POST['photo'];
  6. $filename = time().'.jpg';
  7. file_put_contents(UPLOAD_PATH.'/'.$filename, $photo, true);
  8. echo 'name='.$name.'<br>';
  9. echo 'gender='.$gender.'<br>';
  10. echo '<img src="upload/'.$filename.'">';
  11. ?>
复制代码

3.上传文件

file.php

  1. <?php
  2. $host = 'demo.fdipzone.com';
  3. $port = 80;
  4. $errno = '';
  5. $errstr = '';
  6. $timeout = 30;
  7. $url = '/socket/fileapi.php';
  8. $form_data = array(
  9. 'name' => 'fdipzone',
  10. 'gender' => 'man',
  11. );
  12. $file_data = array(
  13. array(
  14. 'name' => 'photo',
  15. 'filename' => 'photo.jpg',
  16. 'path' =>'photo.jpg'
  17. )
  18. );
  19. // create connect
  20. $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  21. if(!$fp){
  22. return false;
  23. }
  24. // send request
  25. srand((double)microtime()*1000000);
  26. $boundary = "---------------------------".substr(md5(rand(0,32000)),0,10);
  27. $data = "--$boundary\r\n";
  28. // form data
  29. foreach($form_data as $key=>$val){
  30. $data .= "Content-Disposition: form-data; name=\"".$key."\"\r\n";
  31. $data .= "Content-type:text/plain\r\n\r\n";
  32. $data .= rawurlencode($val)."\r\n";
  33. $data .= "--$boundary\r\n";
  34. }
  35. // file data
  36. foreach($file_data as $file){
  37. $data .= "Content-Disposition: form-data; name=\"".$file['name']."\"; filename=\"".$file['filename']."\"\r\n";
  38. $data .= "Content-Type: ".mime_content_type($file['path'])."\r\n\r\n";
  39. $data .= implode("",file($file['path']))."\r\n";
  40. $data .= "--$boundary\r\n";
  41. }
  42. $data .="--\r\n\r\n";
  43. $out = "POST ${url} HTTP/1.1\r\n";
  44. $out .= "Host:${host}\r\n";
  45. $out .= "Content-type:multipart/form-data; boundary=$boundary\r\n"; // multipart/form-data
  46. $out .= "Content-length:".strlen($data)."\r\n";
  47. $out .= "Connection:close\r\n\r\n";
  48. $out .= "${data}";
  49. fputs($fp, $out);
  50. // get response
  51. $response = '';
  52. while($row=fread($fp, 4096)){
  53. $response .= $row;
  54. }
  55. fclose($fp);
  56. $pos = strpos($response, "\r\n\r\n");
  57. $response = substr($response, $pos+4);
  58. echo $response;
  59. ?>
复制代码

fileapi.php

  1. <?php
  2. define('UPLOAD_PATH', dirname(__FILE__).'/upload');
  3. $name = $_POST['name'];
  4. $gender = $_POST['gender'];
  5. $filename = time().'.jpg';
  6. echo 'name='.$name.'<br>';
  7. echo 'gender='.$gender.'<br>';
  8. if(move_uploaded_file($_FILES['photo']['tmp_name'], UPLOAD_PATH.'/'.$filename)){
  9. echo '<img src="upload/'.$filename.'">';
  10. }
  11. ?>
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报