Home  >  Article  >  Backend Development  >  PHP function example to get http content

PHP function example to get http content

WBOY
WBOYOriginal
2016-07-25 08:54:32888browse
  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-urlencodedrn".
  9. "Content-Length: {$length}rn".
  10. "P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"rn".
  11. "cookie: {$cookie}rn".
  12. "Connection: closern";
  13. $options['http']['content'] = $data;
  14. else: // by bbs.it-home.org
  15. $options['http']['header'] = "Content-type: application/x-www-form-urlencodedrn".
  16. "P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"rn".
  17. "cookie: {$cookie}rn".
  18. "Connection: closern";
  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");

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn