欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php获取http内容的函数示例

程序员文章站 2023-12-23 00:01:52
...
  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");
复制代码

上一篇:

下一篇: