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

代码如下:

<?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");

(0)

相关推荐

  • PHP实现取得HTTP请求的原文

    本文实例讲述了PHP实现取得HTTP请求的原文的方法,具体步骤如下: 1. 取得请求行:Method.URI.协议 可以从超级变量$_SERVER中获得,三个变量的值如下: $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL']."\r\n"; 2. 取得所有Header PHP有个内置函数getallheader(),是apache_request_headers()

  • php下获取http状态的实现代码

    逐风整理了两种方式,大家可以自行参考/使用: 复制代码 代码如下: #方式一$ch = curl_init('http://www.jb51.net');curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_exec($ch);echo curl_getinfo($ch, CURLINFO_HTTP_CODE); // 200curl_close($ch); 方式二 复制代码 代码如下: print_r(    get_headers('http://

  • PHP 使用header函数设置HTTP头的示例解析 表头

    如下所示: 复制代码 代码如下: //定义编码  header( 'Content-Type:text/html;charset=utf-8 '); //Atom  header('Content-type: application/atom+xml'); //CSS  header('Content-type: text/css'); //Javascript  header('Content-type: text/javascript'); //JPEG Image  header('Con

  • PHP中模拟处理HTTP PUT请求的例子

    关于HTTP PUT详细介绍请参阅此文:http://www.jb51.net/article/52515.htm. PHP里有$_GET,$_POST,但是没有$_PUT,所以如果需要使用它的话,则你不得不自己模拟一下: 复制代码 代码如下: $_PUT = array(); if ('PUT' == $_SERVER['REQUEST_METHOD']) {      parse_str(file_get_contents('php://input'), $_PUT);  } 通过php:/

  • php获取通过http协议post提交过来xml数据及解析xml

    php 如何获取请求的xml数据,对方通过http协议post提交过来xml数据,php如何获取到这些数据呢? 复制代码 代码如下: <?php $xml_data ='<AATAvailReq1>'. '<Agency>'. '<Iata>1234567890</Iata>'. '<Agent>lgsoftwares</Agent>'. '<Password>mypassword</Password>'

  • php中调用其他系统http接口的方法说明

    使用函数:  file_get_contents($url); 传入接口url及其参数:如 $url="http://192.168.1.1/test.jsp?id=1&type=2"; $ret=file_get_contents($url);

  • PHP实现支持GET,POST,Multipart/form-data的HTTP请求类

    本文实例讲述了PHP实现支持GET,POST,Multipart/form-data的HTTP请求类及其应用,分享给大家供大家参考.具体如下: HttpRequest.class.php类文件如下: <?php /** HttpRequest class, HTTP请求类,支持GET,POST,Multipart/form-data * Date: 2013-09-25 * Author: fdipzone * Ver: 1.0 * * Func: * public setConfig 设置连接

  • php模拟asp中的XmlHttpRequest实现http请求的代码

    类名 :HttpRequest($url="",$method="GET",$useSocket=0) //$url为请求的地址:默认请求方法为GET:$useSocket默认为0,使用fsockopen方法,如果设置为1则使用socket_create方法 方法: open($ip="",$port=-1) //打开同服务器的连接,默认不用设置这两个参数(一个同事在linux用的时候,请求的不是hostname解析的IP,因此加了这两个参数,以

  • 如何使用php判断服务器是否是HTTPS连接

    复制代码 代码如下: if ($_SERVER['HTTPS'] != "on") { echo "This is not HTTPS";}else{ echo "This is HTTPS";}if ($_SERVER['HTTPS'] != "on") { echo "This is not HTTPS";}else{ echo "This is HTTPS";}

  • PHP获取http请求的头信息实现步骤

    PHP手册提供了现成的函数: getallheaders (PHP 4, PHP 5) getallheaders - Fetch all HTTP request headers 说明 array getallheaders ( void ) Fetches all HTTP headers from the current request. This function is an alias for apache_request_headers(). Please read theapach

随机推荐