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

实现获取http内容的php函数分享

程序员文章站 2023-11-26 14:40:58
复制代码 代码如下:

复制代码 代码如下:

<?php
function http_open($url, $data, $cookie = null, $method = "get", $timeout = 60) {
 $options = array();
 $options['http']['method'] = $method;
 $options['http']['user_agent'] = $_server['http_user_agent'];
 $options['http']['timeout'] = $timeout;
 if($method == "post") :
  $length = strlen($data);
  $options['http']['header'] = "content-type: application/x-www-form-urlencoded\r\n".
  "content-length: {$length}\r\n".
  "p3p: cp=\"cura adma deva psao psdo our bus uni pur int dem sta pre com nav otc noi dsp cor\"\r\n".
  "cookie: {$cookie}\r\n".
  "connection: close\r\n";
  $options['http']['content'] = $data;
 else:
  $options['http']['header'] = "content-type: application/x-www-form-urlencoded\r\n".
  "p3p: cp=\"cura adma deva psao psdo our bus uni pur int dem sta pre com nav otc noi dsp cor\"\r\n".
  "cookie: {$cookie}\r\n".
  "connection: close\r\n";
 endif;

 $context = stream_context_create($options);
 return file_get_contents($url, 0, $context);
}
echo http_open("http://localhost/1.php", "username=haowei", "id=5", "post");