fsockopen 模拟 post get 请求

  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. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值