Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php获取http内容的函数示例

php获取http内容的函数示例

发布时间:2017-12-08   编辑:www.jquerycn.cn
分享一个获取http内容的php函数,掌握下php中实现http请求的方法,有需要的朋友参考下。

例子,php获取http请求内容。
 

复制代码 代码示例:

<?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: // by www.jbxue.com
  $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");

您可能感兴趣的文章:
php 获取远程网页内容简单函数
php 获取网站地址的函数代码
memory_get_usage获取当前PHP内存消耗量的例子
php pi函数是什么意思
php页面缓存的例子(减经cpu与mysql负担)
php获得数组长度(元素个数)的方法
php获取远程文件大小的函数
从匿名函数(闭包特性)到 PHP 设计模式之容器模式
php获取字符串的编码格式的函数
php file_get_contents函数抓取页面信息的代码

关键词: http请求   
[关闭]