<?php

/**

* Respose A Http Request

*

* @param string $url

* @param array $post

* @param string $method

* @param bool $returnHeader

* @param string $cookie

* @param bool $bysocket

* @param string $ip

* @param integer $timeout

* @param bool $block

* @return string Response

*/

function httpRequest($url,$post='',$method='GET',$limit=0,$returnHeader=FALSE,$cookie='',$bysocket=FALSE,$ip='',$timeout=15,$block=TRUE) {

$return = '';

$matches = parse_url($url);

!isset($matches['host']) && $matches['host'] = '';

!isset($matches['path']) && $matches['path'] = '';

!isset($matches['query']) && $matches['query'] = '';

!isset($matches['port']) && $matches['port'] = '';

$host = $matches['host'];

$path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';

$port = !empty($matches['port']) ? $matches['port'] : 80;

if(strtolower($method) == 'post') {

$post = (is_array($post) and !empty($post)) ? http_build_query($post) : $post;

$out = "POST $path HTTP/1.0\r\n";

$out .= "Accept: */*\r\n";

//$out .= "Referer: $boardurl\r\n";

$out .= "Accept-Language: zh-cn\r\n";

$out .= "Content-Type: application/x-www-form-urlencoded\r\n";

$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";

$out .= "Host: $host\r\n";

$out .= 'Content-Length: '.strlen($post)."\r\n";

$out .= "Connection: Close\r\n";

$out .= "Cache-Control: no-cache\r\n";

$out .= "Cookie: $cookie\r\n\r\n";

$out .= $post;

} else {

$out = "GET $path HTTP/1.0\r\n";

$out .= "Accept: */*\r\n";

//$out .= "Referer: $boardurl\r\n";

$out .= "Accept-Language: zh-cn\r\n";

$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";

$out .= "Host: $host\r\n";

$out .= "Connection: Close\r\n";

$out .= "Cookie: $cookie\r\n\r\n";

}

$fp = fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);

if(!$fp){

return '';

}else {

$header = $content = '';

stream_set_blocking($fp, $block);

stream_set_timeout($fp, $timeout);

fwrite($fp, $out);

$status = stream_get_meta_data($fp);

if(!$status['timed_out']) {//未超时

while (!feof($fp)) {

$header .= $h = fgets($fp);

if($h && ($h == "\r\n" ||  $h == "\n")){

break;

}

}

$stop = false;

while(!feof($fp) && !$stop) {

$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));

$content .= $data;

if($limit) {

$limit -= strlen($data);

$stop = $limit <= 0;

}

}

}

fclose($fp);

return $returnHeader ? array($header,$content) : $content;

}

}

?>

PHP之httpRequest的更多相关文章

  1. .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别

    WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...

  2. 防刷票机制研究和.NET HttpRequest Proxy

    最近应朋友之约 测试他做的投票网站 防刷票机制能力如何,下面有一些心得和体会. 朋友网站用PHP写的,走的是HttpRequest,他一开始认为IP认证应该就差不多了.但说实话这种很low,手动更换代 ...

  3. python httprequest, locust

    r = self.client.get("/orders", headers = {"Cookie": self.get_user_cookie(user[0] ...

  4. c# WebBrower 与 HttpRequest配合 抓取数据

    今天研究一个功能,发现一个问题. 通过webbrower模拟用户自动登录可以完成,并且可以取到相对应的页面内容. 但是如果页面中通过ajax,动态加载的内容,这种方式是取不到的,于是用到了httpRe ...

  5. Asp.net中HttpRequest.Params与Reques.Item之异同

    今天才注意到HttpRequest.Params与HttpRequest.Item这两个玩意竟然有微妙的不同.上午的时候同事被坑了发现这玩意的说明还真微妙. 场景再现: 前台提交一个POST请求到后台 ...

  6. HttpRequest重写,解决资源战胜/链接超时/分块下载事件通知 问题。

    /************************************************************************************** 文 件 名: WebRe ...

  7. 对象化的Http和请求对象HttpRequest

    在面向对象的语言中,有种“万物皆对象”的说法.在上篇文章中介绍了HttpRuntime类,在该类收到请求之后,立即通过HttpWorkerRequest工作者对象对传递的参数进行分析和分解,创建方便网 ...

  8. ASP.Net核心对象HttpRequest

    描述context. Request["username"]; 通过这种方式,能够得到一个HttpRequest对象.HttpRequest对象描述了,关于请求的相关信息,我们可以 ...

  9. .net学习笔记----HttpRequest类

    一.HttpRequest的作用 HttpRequest的作用是令到Asp.net能够读取客户端发送HTTP值.比如表单.URL.Cookie传递过来的参数. 返回字符串的那些值就不说了,那些基本上都 ...

  10. Win7下 httpRequest带证书请求https网站

    常规情况下创建Web请求,并获取请求数据的代码如下: WebRequest req = WebRequest.Create(url); req.Timeout = 15000; WebResponse ...

随机推荐

  1. JDK下载与安装、 Eclipse下载与使用的总结心得_20173311118_牛明旺

     一.JDK下载与安装心得: ① 从官网http://www.oracl.com/technetwork/java上下载JDK,注意一定要同意该网站上的协议,否则下载不了(即点击“Accept Lic ...

  2. 1.2 Percona XtraDB Cluster Limitations

    摘要: 出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该 ...

  3. 剑指offer——面试题32.1:分行从上到下打印二叉树

    void BFSLayer(BinaryTreeNode* pRoot) { if(pRoot==nullptr) return; queue<BinaryTreeNode*> pNode ...

  4. Ngin 简单配置文件

    #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...

  5. 用sql语句导出oracle中的存储过程和函数

    用sql语句导出oracle中的存储过程和函数: SET echo off ; SET heading off ; SET feedback off ; SPOOL 'C:/PRC.SQL' repl ...

  6. Java GC 垃圾回收算法 内存分配

    垃圾回收(Garbage Collection, GC)是Java不同于c与c++的重要特性之一. 他帮助Java自动清空堆中不再使用的对象. 由于不需要手动释放内存,程序员在编程中也可以减少犯错的机 ...

  7. R语言列表list函数

    列表是R语言中的对象,它包含不同类型的元素,比如 - 数字,字符串,向量和另一个列表等.一个列表还可以包含一个矩阵或一个函数作为它的元素.使用list()函数创建列表. 创建一个列表 下面是一个例子来 ...

  8. JS数组和函数 小记

    数组 JS中的数组来自window,是一个全局的对象,typeof的值是'object'. 创建数组: 1.Array(3):当只传一个值的时候,会生成一个长度为该数值的空数组. 2.Array(3, ...

  9. javascript图形动画设计--画简单正弦波

        <!doctype html> <html> <head> <meta charset="utf-8"> <title ...

  10. [转]Sql Server 分页存储过程

    本文转自: 版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking  本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且 ...