PHP 使用 Post 方式请求网页数据入门实例

前端之家收集整理的这篇文章主要介绍了PHP 使用 Post 方式请求网页数据入门实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP 以 Post 方式请求网页数据感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
  1. /**
  2. * 以 Post 方式请求网页数据
  3. *
  4. * @param
  5. * @arrange 网: jb51.cc
  6. * Post 方式请求网页数据
  7. *
  8. * @param string $url 网页地址
  9. * @prarm string $host 主机
  10. * @param string $session 会话值
  11. * @prarm string $type 类型(POST、GET)
  12. * @prarm string $port 端口
  13. * @prarm string $data 数据
  14. */
  15. function getPageConent( $url,$host,$data = "",$session = "",$type = "POST",$port = "") {
  16. if( empty($port) ) $port = 80;
  17. /* 请求数据 */
  18. $post_data = $data;
  19. $lenght = strlen($post_data);
  20. $headers = "{$type} {$url} HTTP/1.1\r\n";
  21. $headers .= "Accept: * /*\r\n";
  22. $headers .= "Content-Type: application/x-www-form-urlencoded\r\n";
  23. $headers .= "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6; CIBA; .NET CLR 4.0.20506)\r\n";
  24. if($session != "" ) $headers .= "Cookie:JSESSIONID={$session}\r\n";
  25. $headers .= "Host: {$host}:{$port}\r\n";
  26. $headers .= "Content-Length: {$lenght}\r\n";
  27. $headers .= "Connection: Close\r\n\r\n";
  28. $headers .= $post_data;
  29. if( $fp = fsockopen( $host,$port,$errno,$errstr,100) ) {
  30. fwrite($fp,$headers);
  31. $header = fread($fp,1024);
  32. $content = fread($fp,1024);
  33. $content .= fread($fp,1024);
  34. $content .= fread($fp,1024);
  35. fclose($fp);
  36. }
  37. if( $data != "" ) {
  38. echo $headers;
  39. echo "<hr />";
  40. echo $header;
  41. echo "<hr />";
  42. echo $content;
  43. echo "<hr />";
  44. exit;
  45. } else {
  46. return $content;
  47. }
  48. }
  49. /*** 来自编程之家 jb51.cc(jb51.cc) ***/

猜你在找的PHP相关文章