当前位置:Gxlcms > PHP教程 > php获取http内容的函数示例

php获取http内容的函数示例

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

  1. function http_open($url, $data, $cookie = null, $method = "GET", $timeout = 60) {

  2. $options = array();
  3. $options['http']['method'] = $method;
  4. $options['http']['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  5. $options['http']['timeout'] = $timeout;
  6. if($method == "POST") :
  7. $length = strlen($data);
  8. $options['http']['header'] = "Content-type: application/x-www-form-urlencoded\r\n".
  9. "Content-Length: {$length}\r\n".
  10. "P3P: CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"\r\n".
  11. "cookie: {$cookie}\r\n".
  12. "Connection: close\r\n";
  13. $options['http']['content'] = $data;
  14. else: // by bbs.it-home.org
  15. $options['http']['header'] = "Content-type: application/x-www-form-urlencoded\r\n".
  16. "P3P: CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"\r\n".
  17. "cookie: {$cookie}\r\n".
  18. "Connection: close\r\n";
  19. endif;

  20. $context = stream_context_create($options);

  21. return file_get_contents($url, 0, $context);
  22. }
  23. echo http_open("http://localhost/1.php", "username=haowei", "id=5", "POST");

人气教程排行