PHP

php socket模拟POST GET请求 fsockopen版

作者:admin110 来源:web交流网 2016-08-16 23:28 浏览:0 我要评论(0)

Php代码functionhttpRequestGET($url){$url2=parse_url($url);$url2["path"]=($url2["path"]==""?" ":$url2["path"]);$url2["port"]=($url2

Php代码  收藏代码
  1. function httpRequestGET($url){  
  2. $url2 = parse_url($url);  
  3. $url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);  
  4. $url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]);  
  5. $host_ip = @gethostbyname($url2["host"]);  
  6. $fsock_timeout=20;  
  7. if(($fsock = fsockopen($host_ip, 80, $errno$errstr$fsock_timeout)) < 0){  
  8. return false;  
  9. }  
  10.   
  11. $request = $url2["path"] . ($url2["query"] != "" ? "?" . $url2["query"] : "") . ($url2["fragment"] != "" ? "#" . $url2["fragment"] : "");   
  12. $in = "GET " . $request . " HTTP/1.0\r\n";  
  13. $in .= "Accept: */*\r\n";  
  14. $in .= "User-Agent: Payb-Agent\r\n";  
  15. $in .= "Host: " . $url2["host"] . "\r\n";  
  16. $in .= "Connection: Close\r\n\r\n";  
  17. if(!@fwrite($fsock$instrlen($in))){  
  18. fclose($fsock);  
  19. return false;  
  20. }  
  21. unset($in);  
  22.   
  23. $out = "";   
  24. while($buff = @fgets($fsock, 2048)){  
  25. $out .= $buff;  
  26. }  
  27. fclose($fsock);  
  28. $pos = strpos($out"\r\n\r\n");  
  29. $head = substr($out, 0, $pos);    //http head  
  30. $status = substr($head, 0, strpos($head"\r\n"));    //http status line  
  31. $body = substr($out$pos + 4, strlen($out) - ($pos + 4));//page body  
  32. if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/"$status$matches)){  
  33. if(intval($matches[1]) / 100 == 2){  
  34. return $body;   
  35. }else{  
  36. return false;  
  37. }  
  38. }else{  
  39. return false;  
  40. }  
  41. }  
  42. function httpRequestPOST($url,$post_data){  
  43. $url2 = parse_url($url);  
  44. $url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);  
  45. $url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]);  
  46. $host_ip = @gethostbyname($url2["host"]);  
  47. $fsock_timeout=20;//秒  
  48. if(($fsock = fsockopen($host_ip, 80, $errno$errstr$fsock_timeout)) < 0){  
  49. return false;  
  50. }  
  51.   
  52. $request = $url2["path"] . ($url2["query"] != "" ? "?" . $url2["query"] : "") . ($url2["fragment"] != "" ? "#" . $url2["fragment"] : "");   
  53.   
  54. $needChar = false;   
  55.   
  56. foreach($post_data as $key => $val) {   
  57.   
  58. $post_data2 .= ($needChar ? "&" : "") . urlencode($key) . "=" . urlencode($val);   
  59. $needChar = true;  
  60. }  
  61. $in = "POST " . $request . " HTTP/1.0\r\n";  
  62. $in .= "Accept: */*\r\n";  
  63. $in .= "Host: " . $url2["host"] . "\r\n";  
  64. $in .= "User-Agent: Lowell-Agent\r\n";  
  65. $in .= "Content-type: application/x-www-form-urlencoded\r\n";  
  66. $in .= "Content-Length: " . strlen($post_data2) . "\r\n";  
  67. $in .= "Connection: Close\r\n\r\n";  
  68. $in .= $post_data2 . "\r\n\r\n";  
  69.   
  70. unset($post_data2);   
  71. if(!@fwrite($fsock$instrlen($in))){  
  72. fclose($fsock);  
  73. return false;  
  74. }  
  75. unset($in);  
  76.   
  77. $out = "";   
  78. while($buff = fgets($fsock, 2048)){  
  79. $out .= $buff;  
  80. }  
  81.   
  82. fclose($fsock);   
  83. $pos = strpos($out"\r\n\r\n");  
  84. $head = substr($out, 0, $pos);    //http head  
  85. $status = substr($head, 0, strpos($head"\r\n"));    //http status line  
  86. $body = substr($out$pos + 4, strlen($out) - ($pos + 4));//page body  
  87. if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/"$status$matches)){  
  88. if(intval($matches[1]) / 100 == 2){  
  89. return $body;  
  90. }else{  
  91. return false;  
  92. }  
  93. }else{  
  94. return false;  
  95. }  
  96. }  

 

Php代码  收藏代码
  1. $post_data = array("name"=>"xd","sex"=>"man");  
  2. httpRequestPOST("http://localhost/post.php",$post_data);  

 

socket写的顺序:


POST /post.php HTTP/1.0
Accept: */*
Host: localhost
User-Agent: Lowell-Agent
Content-type: application/x-www-form-urlencoded
Content-Length: 15
Connection: Close
name=xd&sex=man


普通POST的结果演示:

POST/?username=111&password=222HTTP/1.1
Host:127.0.0.1:8000
User-Agent:Mozilla/5.0(Windows;U;WindowsNT5.2;zh-CN;rv:1.9.0.1)Gecko/2008070208Firefox/3.0.1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language:zh-cn,zh;q=0.5
Accept-Encoding:gzip,deflate
Accept-Charset:gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive:300
Connection:keep-alive
Referer:http://127.0.0.1:8000/?username=111&password=222
Content-Type:application/x-www-form-urlencoded
Content-Length:25
username=111&password=222

 


GET的运行结果演示:

 

GET/?username=111&password=222HTTP/1.1
Host:127.0.0.1:8000
User-Agent:Mozilla/5.0(Windows;U;WindowsNT5.2;zh-CN;rv:1.9.0.1)Gecko/2008070208Firefox/3.0.1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language:zh-cn,zh;q=0.5
Accept-Encoding:gzip,deflate
Accept-Charset:gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive:300
Connection:keep-alive
Referer:http://127.0.0.1:8000/?username=1&password=2


POST文件上传的结果演示:

 

POST/?username=111&password=222HTTP/1.1
Host:127.0.0.1:8000
User-Agent:Mozilla/5.0(Windows;U;WindowsNT5.2;zh-CN;rv:1.9.0.1)Gecko/2008070208Firefox/3.0.1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language:zh-cn,zh;q=0.5
Accept-Encoding:gzip,deflate
Accept-Charset:gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive:300
Connection:keep-alive
Referer:http://127.0.0.1:8000/?username=111&password=222
Content-Type:multipart/form-data;boundary=---------------------------23757983230932
Content-Length:1704
-----------------------------23757983230932
Content-Disposition:form-data;name="phototitle"
12
-----------------------------23757983230932

转载请注明出处。

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源;3.作者投稿可能会经我们编辑修改或补充。

网友点评
评论(已有0条评论)
还没有评论,快来抢沙发吧!
新闻
  • 新闻
  • 软件
精彩导读