出于安全的考虑,常常会关闭fopen, file_get_contents, 也就是会把 allow_url_fopen设置为OFF,如果想要继续使用这些函数,就可以用到这个类。

<?php/* used for the transmission RPC connection * and the SABnzbd+ file submit *//***************************************************************************Browser Emulating file functions v2.0.1-torrentwatch
(c) Kai Blankenhorn
www.bitfolge.de/browseremulator
kaib@bitfolge.deThis program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.****************************************************************************Changelog:v2.0.1-torrentwatch2 by Erik Bernhardsonmulti-part post with file submitv2.0.1-torrentwatch by Erik Bernhardsonconverted file() to file_get_contents()converted lastResponse to string from array to mimic file_get_contentsadded gzip compression supportv2.0.1fixed authentication bugadded global debug switchv2.0   03-09-03added a wrapper class; this has the advantage that you no longer needto specify a lot of parameters, just call the methods to seteach optionadded option to use a special port number, may be given by setPort oras part of the URL (e.g. server.com:80)added getLastResponseHeaders()v1.5added Basic HTTP user authorizationminor optimizationsv1.0initial release***************************************************************************//**
* BrowserEmulator class. Provides methods for opening urls and emulating
* a web browser request.
**/
class BrowserEmulator {var $headerLines = Array();var $postData = Array();var $multiPartPost = False;var $authUser = "";var $authPass = "";var $port;var $lastResponse = '';var $lastRequest = '';var $debug = false;var $customHttp = False;public function BrowserEmulator() {$this->resetHeaderLines();$this->resetPort();}/*** Adds a single header field to the HTTP request header. The resulting header* line will have the format* $name: $value\n**/public function addHeaderLine($name, $value) {$this->headerLines[$name] = $value;}/*** Deletes all custom header lines. This will not remove the User-Agent header field,* which is necessary for correct operation.**/public function resetHeaderLines() {$this->headerLines = Array();/*******************************************************************************//**************   YOU MAX SET THE USER AGENT STRING HERE   *******************//*                                                   *//* default is "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",         *//* which means Internet Explorer 6.0 on WinXP                       */$this->headerLines["User-Agent"] = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10';/*******************************************************************************//*** Set default to accept gzip encoded files*/$this->headerLines["Accept-Encoding"] = "*/*";}/*** Add a post parameter. Post parameters are sent in the body of an HTTP POST request.**/public function addPostData($name, $value = '') {$this->postData[$name] = $value;}/*** Deletes all custom post parameters.**/public function resetPostData() {$this->postData = Array();}public function handleMultiPart() {$boundry = '----------------------------795088511166260704540879626';$this->headerLines["Accept"] = ' text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';$this->headerLines["Connection"] = 'Close';$this->headerLines["Content-Type"] = "multipart/form-data; boundary=$boundry";$out = '';foreach($this->postData as $item => $data) {if(is_array($data)) {$out .= "--$boundry\r\n"."Content-Disposition: form-data; name=\"$item\"; filename=\"{$data['filename']}\"\r\n"."Content-Type: application/octet-stream\r\n"."\r\n".$data['contents']."\r\n";} else {$out .= "--$boundry\r\n"."Content-Disposition: form-data; name=\"$item\"\r\n"."\r\n".$data."\r\n";}}$out .= "--{$boundry}--\r\n";return $out;}/*** Sets an auth user and password to use for the request.* Set both as empty strings to disable authentication.**/public function setAuth($user, $pass) {$this->authUser = $user;$this->authPass = $pass;}/*** Selects a custom port to use for the request.**/public function setPort($portNumber) {$this->port = $portNumber;}/*** Resets the port used for request to the HTTP default (80).**/public function resetPort() {$this->port = 80;}/*** Parse any cookies set in the URL, and return the trimed string**/public function preparseURL($url) {if($cookies = stristr($url, ':COOKIE:')) {$url = rtrim(substr($url, 0, -strlen($cookies)), '&');$this->addHeaderLine("Cookie", '$Version=1; '.strtr(substr($cookies, 8), '&', ';'));}return $url;}/*** Make an fopen call to $url with the parameters set by previous member* method calls. Send all set headers, post data and user authentication data.* Returns a file handle on success, or false on failure.**/public function fopen($url) {$url = $this->preparseURL($url);$this->lastResponse = Array();$parts = parse_url($url);$protocol = $parts['scheme'];$server = $parts['host'];$port = $parts['port'];$path = $parts['path'];if(isset($parts['query'])) {$path .= '?'.$parts['query'];}if($protocol == 'https') {// TODO: https is locked to port 443, why?$server = 'ssl://'.$server;$this->setPort(443);} elseif ($port!="") {$this->setPort($port);}if ($path=="") $path = "/";$socket = false;$socket = fsockopen($server, $this->port);if ($socket) {if ($this->authUser!="" && $this->authPass!="") {$this->headerLines["Authorization"] = "Basic ".base64_encode($this->authUser.":".$this->authPass);}if($this->customHttp)$request = $this->customHttp." $path\r\n";elseif (count($this->postData)==0)$request = "GET $path HTTP/1.0\r\n";else$request = "POST $path HTTP/1.1\r\n";$request .= "Host: {$parts['host']}\r\n";if ($this->debug) echo $request;if (count($this->postData)>0) {if($this->multiPartPost) {$PostString = $this->handleMultiPart();} else {$PostStringArray = Array();foreach ($this->postData AS $key=>$value) {if(empty($value))$PostStringArray[] = $key;else$PostStringArray[] = "$key=$value";}$PostString = join("&", $PostStringArray);}$this->headerLines["Content-Length"] = strlen($PostString);}foreach ($this->headerLines AS $key=>$value) {if ($this->debug) echo "$key: $value\n";$request .= "$key: $value\r\n";}if ($this->debug) echo "\n";$request .= "\r\n";if (count($this->postData)>0) {$request .= $PostString;}}$this->lastRequest = $request;for ($written = 0; $written < strlen($request); $written += $fwrite) {$fwrite = fwrite($socket, substr($request, $written));if (!$fwrite) {break;}}if ($this->debug) echo "\n";if ($socket) {$line = fgets($socket);if ($this->debug) echo $line;$this->lastResponse .= $line;$status = substr($line,9,3);while (trim($line = fgets($socket)) != ""){if ($this->debug) echo "$line";$this->lastResponse .= $line;if ($status=="401" AND strpos($line,"WWW-Authenticate: Basic realm=\"")===0) {fclose($socket);return FALSE;}}}return $socket;}/*** Make an file call to $url with the parameters set by previous member* method calls. Send all set headers, post data and user authentication data.* Returns the requested file as a string on success, or false on failure.**/public function file_get_contents($url) {if(file_exists($url)) // local filereturn file_get_contents($url);$file = '';$socket = $this->fopen($url);if ($socket) {while (!feof($socket)) {$file .= fgets($socket);}} else {Yii::log('Browser Emulator: file_get_contents bad socket', CLogger::LEVEL_ERROR);return FALSE;}fclose($socket);if(strstr($this->lastResponse, 'Content-Encoding: gzip') !== FALSE) {if(function_exists('gzinflate')) {$file = gzinflate(substr($file,10));if($this->debug) echo "Result file: ".$file;}}return $file;}/*** Simulate a file() call by exploding file_get_contents()**/public function file($url) {$data = $this->file_get_contents($url);if($data)return explode('\n', $data);return False;}public function getLastResponseHeaders() {return $this->lastResponse;}
}

实例:

$be = new BrowserEmulator();$output = $be->file_get_contents("http://tvbinz.net/rss.php");
$response = $be->getLastResponseHeaders();echo $output;

来源: http://code.google.com/p/torrentwatch/source/browse/branches/yii/protected/components/downloadClients/browserEmulator.php?spec=svn780&r=780

关联:

PHP获取远程文件内容

function curl_get_contents($url)
{$dir = pathinfo($url);$host = $dir['dirname'];$refer = $host.'/';$ch = curl_init($url);curl_setopt ($ch, CURLOPT_REFERER, $refer);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);$data = curl_exec($ch);curl_close($ch);return $data;
}

获取远程文件内容之浏览器模拟器(BrowserEmulator)相关推荐

  1. php curl 采集文件,curl获取远程文件内容

    /** 获取远程文件内容 @param $url 文件http地址 */ function fopen_url($url) { if (function_exists('file_get_conten ...

  2. php 读写远程文件内容,php获取远程文件内容的函数

    一个简单的php获取远程文件内容的函数代码,兼容性强.直接调用就可以轻松获取远程文件的内容,使用这个函数也可获取图片.代码如下: /** * 读远程内容 * @return string */ fun ...

  3. PHP获取远程文件内容

    一. 介绍 只要在 php.ini 文件中激活了 allow_url_fopen 选项,您可以在大多数需要用文件名作为参数的函数中使用 HTTP 和 FTP URL 来代替文件名.同时,您也可以在 i ...

  4. java 读取远程文件并让浏览器下载

    java 读取远程文件并让浏览器下载 @RequestMapping("/downLoadFile")@ResponseBodypublic ResponseEntity<b ...

  5. java 读取 远程文件_利用JAVA获取远程文件及使用断点续传 供学习者使用

    闲来没事,就做做,程序还是要多写才好@ 原理不说,文件方面及I/O方面,去BAIDU一下就知道,断点续传的原理也很简单,就是在原来已经下载的基础之上继续下载就可以了,用到了这么关键的一句:urlc.s ...

  6. Java 获取远程文件的大小

    我们应该如何获取远程文件的大小的呢? 代码如下 import java.net.URL; import java.net.URLConnection;public class Main {public ...

  7. 利用JAVA获取远程文件及使用断点续传 供学习者使用

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 闲来没事 ...

  8. linux下qt浏览word文件内容,Qt获取office文件内容

    Qt获取office文件内容 需要获取word文件的文件内容.网上找了好久,大部分都是excel的.而word的很少.所以在这里记录一下,方便大家查阅和自己使用. 使用的Qt版本是5.4.2 . 下面 ...

  9. scp命令获取远程文件

    一.scp是什么? scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的,可能会稍微影响 ...

最新文章

  1. Linux上安装paramiko模块
  2. background 距离右边固定距离
  3. 记录一下添加查询场地坐标功能中修改判断条件和画点的大小
  4. listview 滑动更改标题
  5. 终极版Python学习教程:一篇文章讲清楚Python虚拟环境
  6. 虚拟机中使用Samba实现文件共享,并在win10上创建映射网络驱动器
  7. springboot项目 tomcat8.x 频繁宕机 原因分析
  8. [转载] Java中的字符串处理
  9. 歪枣网数据库设计-千万级别海量数据查询效率优化
  10. explain for connection用法
  11. express 设置header解决跨域问题
  12. C语言常见题目汇总(不断更新)(建议收藏)
  13. Oracle数据库练习题(3)
  14. C++ 依赖倒置原则
  15. 基于matlab的字符识别系统
  16. office转换为还原度高的html,使用Aspose把office文件转换为Html文件及生成文件浏览乱码的解决...
  17. 什么是智能无损网络?
  18. java http请求图片_Java上传带图片的Http请求详解
  19. Linkedin领英如何批量撤回邀请
  20. java开发名言_java实现收藏名言语句台词的app

热门文章

  1. c语言写数码管,各位大神,如何用C语言实现在数码管上实现1234同时亮
  2. 关系 base_weather 不存在_国培教育-2020江苏公务员考试:反对关系真的好用吗?...
  3. 中职计算机技术教学计划,中职计算机教学计划
  4. RabbitMQ安装FAQ(接前面一篇)
  5. 通过kubernetes release制作k8s rpm包
  6. 我就传个图片都不通过迈
  7. CNN是不是一种局部self-attention?
  8. 外部表在Hive中的使用
  9. [电工] 比较电路、反向滞回电路,正向滞回电路预习题
  10. JAVA 中JDK下载安装