当前位置:Gxlcms > PHP教程 > PHP模拟POST的步骤

PHP模拟POST的步骤

时间:2021-07-01 10:21:17 帮助过:2人阅读

PHP模拟POST的方法

PHP主动POST数据到JSP

采用哪种方式好?

最好有例子,谢谢

------解决方案--------------------
后台模拟 哪种都差不多 最常用的curl模块
------解决方案--------------------
PHP code
  1. function post_url($url, $post = "", $host = "www.ydtuiguang.com", $referrer = 'http://www.ydtuiguang.com/', $proxy = -1){
  2. if(function_exists("curl_init")){
  3. $ch = @curl_init();
  4. @curl_setopt($ch, CURLOPT_URL, $url);
  5. if(!empty($proxy["address"]))
  6. @curl_setopt($ch, CURLOPT_PROXY, strpos($proxy["address"], "http") === 0 ? $proxy["address"] : "http://".$proxy["address"]);
  7. if(!empty($proxy["account"]) && !empty($proxy["password"]))
  8. @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy["account"].":".$proxy["password"]);
  9. @curl_setopt($ch, CURLOPT_REFERER, $referrer);
  10. @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)");
  11. @curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_PATH);
  12. @curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_PATH);
  13. @curl_setopt($ch, CURLOPT_HEADER, 0);
  14. @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  15. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  16. @curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
  17. if (!empty($post)) {
  18. @curl_setopt($ch, CURLOPT_POST, 1);
  19. @curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  20. }
  21. $result = @curl_exec($ch);
  22. @curl_close($ch);
  23. }elseif(function_exists("fsockopen")){
  24. $httpheader = "POST ".$url." HTTP/1.1\r\n";
  25. $httpheader .= "Accept: */*\r\n";
  26. $httpheader .= "Accept-Language: zh-cn\r\n";
  27. $httpheader .= "Referer: ".$referrer."\r\n";
  28. $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";
  29. $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";
  30. $httpheader .= "Host: ".$host."\r\n";
  31. $httpheader .= "Content-Length: ".strlen($post)."\r\n";
  32. $httpheader .= "Connection: Keep-Alive\r\n";
  33. $httpheader .= "\r\n";
  34. $httpheader .= $post;
  35. $fd = fsockopen($host, 80);
  36. fwrite($fd, $httpheader);
  37. $result = "";
  38. while(!feof($fd)){
  39. $result .= fread($fd, 8192);
  40. }
  41. fclose($fd);
  42. }elseif(function_existes('file_get_contents')){
  43. $httpheader = "POST ".$url." HTTP/1.1\r\n";
  44. $httpheader .= "Accept: */*\r\n";
  45. $httpheader .= "Accept-Language: zh-cn\r\n";
  46. $httpheader .= "Referer: ".$referrer."\r\n";
  47. $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";
  48. $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";
  49. $httpheader .= "Host: ".$host."\r\n";
  50. $httpheader .= "Content-Length: ".strlen($post)."\r\n";
  51. $httpheader .= "Connection: Keep-Alive\r\n";
  52. $opts = array(
  53. 'http'=>array(
  54. 'method'=>"POST",
  55. 'header'=>$httpheader,
  56. 'content'=>$post
  57. )
  58. );
  59. $context = stream_context_create($opts);
  60. $result = file_get_contents($url, 'r', $context);
  61. }
  62. return $result;
  63. }
  64. <br><font color="#e78608">------解决方案--------------------</font><br><fieldset><legend>探讨</legend><br><br>后台模拟 哪种都差不多 最常用的curl模块<br> </fieldset>

人气教程排行