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

来源:互联网 发布:高性能mysql 知乎 编辑:程序博客网 时间:2024/06/05 19:40

1.GET

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

2.POST

post.php
[php] view plain copy
  1. <?php  
  2. $host = 'demo.fdipzone.com';  
  3. $port = 80;  
  4. $errno = '';  
  5. $errstr = '';  
  6. $timeout = 30;  
  7. $url = '/socket/postapi.php';  
  8.   
  9. $param = array(  
  10.     'name' => 'fdipzone',  
  11.     'gender' => 'man',  
  12.     'photo' => file_get_contents('photo.jpg')  
  13. );  
  14.   
  15. $data = http_build_query($param);  
  16.   
  17. // create connect  
  18. $fp = fsockopen($host$port$errno$errstr$timeout);  
  19.   
  20. if(!$fp){  
  21.     return false;  
  22. }  
  23.   
  24. // send request  
  25. $out = "POST ${url} HTTP/1.1\r\n";  
  26. $out .= "Host:${host}\r\n";  
  27. $out .= "Content-type:application/x-www-form-urlencoded\r\n";  
  28. $out .= "Content-length:".strlen($data)."\r\n";  
  29. $out .= "Connection:close\r\n\r\n";  
  30. $out .= "${data}";  
  31.   
  32. fputs($fp$out);  
  33.   
  34. // get response  
  35. $response = '';  
  36. while($row=fread($fp, 4096)){  
  37.     $response .= $row;  
  38. }  
  39.   
  40. fclose($fp);  
  41.   
  42. $pos = strpos($response"\r\n\r\n");  
  43. $response = substr($response$pos+4);  
  44.   
  45. echo $response;  
  46. ?>  
postapi.php
[php] view plain copy
  1. <?php  
  2. define('UPLOAD_PATH', dirname(__FILE__).'/upload');  
  3.   
  4. $name = $_POST['name'];  
  5. $gender = $_POST['gender'];  
  6. $photo = $_POST['photo'];  
  7.   
  8. $filename = time().'.jpg';  
  9. file_put_contents(UPLOAD_PATH.'/'.$filename$photo, true);  
  10.   
  11. echo 'name='.$name.'<br>';  
  12. echo 'gender='.$gender.'<br>';  
  13. echo '<img src="upload/'.$filename.'">';  
  14. ?>  

3.上传文件

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





请求


<?php//fsockopen参数$host = 'www.aaa.com'; //域名$port = 80; //端口$errno = ''; //错误信息$errstr = '';//错误信息已字符串形式返回$timeout = 30; //请求时间$url = '/getapi.php'; //请求地址//参数$param = array(    'name' => '123',    'sex' => 'man');//请求地址 $key=$val &  $key= $val$url = $url.'?'.http_build_query($param);// 建立链接$fp = fsockopen($host, $port, $errno, $errstr, $timeout);//是否正确建立链接if(!$fp){    return false;}// 发送请求$out = "GET $url HTTP/1.1\r\n";$out .= "Host: $host\r\n";$out .= "Connection:close\r\n\r\n";fwrite($fp, $out);//响应信息$response = '';while($row=fread($fp, 1024)){    $response .= $row;}//echo $response[241];die;//echo strlen($response);die;//关闭链接fclose($fp);$pos = strpos($response, "\r\n\r\n");$response = substr($response, $pos+6,-6);echo $response ;

响应


<?php$name = $_GET['name'];$sex = $_GET['sex'];echo 'name='.$name.'<br>';echo 'sex='.$sex;



PHP利用fsockopen POST HTTP请求(URL)并获取返回值


  1. <?php 
  2.   $srv_ip = '192.168.1.5';//你的目标服务地址. 
  3.   $srv_port = 80;//端口 
  4.   $url = 'http://localhost/fsock.php'; //接收你post的URL具体地址  
  5.   $fp = ''
  6.   $errno = 0;//错误处理 
  7.   $errstr = '';//错误处理 
  8.   $timeout = 10;//多久没有连上就中断 
  9.   $post_str = "username=demo&password=hahaha";//要提交的内容. 
  10.   //打开网络的 Socket 链接。 
  11.   $fp = fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout); 
  12.   if (!$fp){ 
  13.    echo('fp fail'); 
  14.   } 
  15.   $content_length = strlen($post_str); 
  16.   $post_header = "POST $url HTTP/1.1\r\n"
  17.   $post_header .= "Content-Type: application/x-www-form-urlencoded\r\n"
  18.   $post_header .= "User-Agent: MSIE\r\n"
  19.   $post_header .= "Host: ".$srv_ip."\r\n"
  20.   $post_header .= "Content-Length: ".$content_length."\r\n"
  21.   $post_header .= "Connection: close\r\n\r\n"
  22.   $post_header .= $post_str."\r\n\r\n"
  23.   fwrite($fp,$post_header); 
  24.  
  25.   $inheader = 1; 
  26.   while(!feof($fp)){//测试文件指针是否到了文件结束的位置 
  27.    $line = fgets($fp,1024); 
  28.    //去掉请求包的头信息 
  29.    if ($inheader && ($line == "\n" || $line == "\r\n")) { 
  30.          $inheader = 0; 
  31.     } 
  32.     if ($inheader == 0) { 
  33.       echo $line
  34.     } 
  35.   } 
  36.   fclose($fp); 
  37.   unset ($line); 
  38. ?> 

 简要说明:代码第二行是你的IP地址或域名,第四行是你要POST的页面的具体地址,本例用的是fsock.PHP,fsock.php内容如下:

  1. <?php 
  2.     echo "username:".$_POST['username']."<br/>"
  3.     echo "password:".$_POST['password']; 
  4. ?> 

结果为:

username:demo


password:hahaha


0 0
原创粉丝点击