php中fsockopen采集网页内容实例

 更新时间:2016年11月25日 16:22  点击:1299
fsockopen是php中一个比较实用的函数了,下面我来介绍利用fsockopen函数来采集网页的程序,有需要的朋友可参考。

用法
int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]);

一个采集网页实例

 代码如下 复制代码

<?php
function get_url ($url,$cookie=false)
{
$url = parse_url($url);
$query = $url[path].”?”.$url[query];
echo “Query:”.$query;
$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$request = “GET $query HTTP/1.1rn”;
$request .= “Host: $url[host]rn”;
$request .= “Connection: Closern”;
if($cookie) $request.=”Cookie:   $cookien”;
$request.=”rn”;
fwrite($fp,$request);
while(!@feof($fp)) {
$result .= @fgets($fp, 1024);
}
fclose($fp);
return $result;
}
}
//获取url的html部分,去掉header
function GetUrlHTML($url,$cookie=false)
{
$rowdata = get_url($url,$cookie);
if($rowdata)
{
$body= stristr($rowdata,”rnrn”);
$body=substr($body,4,strlen($body));
return $body;
}

    return false;
}
?>

被禁用后的解决方法

服务器同时禁用了fsockopen pfsockopen,那么用其他函数代替,如stream_socket_client()。注意:stream_socket_client()和fsockopen()的参数不同。

fsockopen( 替换为 stream_socket_client( ,然后,将原fsockopen函数中的端口参数“80”删掉,并加到$host。

 代码如下 复制代码

$fp = fsockopen($host, 80, $errno, $errstr, 30);

$fp = fsockopen($host, $port, $errno, $errstr, $connection_timeout);
修改后:
$fp = stream_socket_client("tcp://".$host."80", $errno, $errstr, 30);

$fp = stream_socket_client("tcp://".$host.":".$port, $errno, $errstr, $connection_timeout);

在php中我们也有可以直接来操作ftp,然后利用php实现与ftp一样的文件上传与下载文件的功能哦,下面我来介绍一个完整的实例。

一、LycFtpAbstract.class.php   FTP基类

 代码如下 复制代码
<?php
    /*  author:凹凸曼(lyc)
    /*  email: jar-c@163.com
    /*  time : 2011-04-22
    */
 
abstract class Lyc_Ftp_Abstract {
 
    protected $ftpobj=null;
    protected $host='';
    protected $user='anonymous';
    protected $pwd='';
    protected $mode=FTP_BINARY;
    protected $port=21;
    protected $timeout=90;
 
    protected $pasv=TRUE;
 
    protected function init(){
 
    }
    /**
    * 建立ftp连接
    *
    */
    protected function connect(){
       $this->ftpobj=@ftp_connect($this->host,$this->port,$this->timeout);
       if(null==$this->ftpobj){
        require_once 'Lyc/Ftp/Exception.class.php';
       throw new Lyc_Ftp_Exception("FTP ERROR : Couldn't connect to $this->host");
       }
    }
    /**
    * 建立ssl ftp连接
    *
    */
    protected function connectSsl(){
       $ftpobj=@ftp_ssl_connect($this->host,$this->port,$this->timeout);
       if(null==$ftpobj){
        require_once 'Lyc/Ftp/Exception.class.php';
       throw new Lyc_Ftp_Exception("FTP ERROR : Couldn't connect to $this->host");
       }
    }
    /**
    * 登录验证ftp 及设置模式
    *
    */
    protected function login(){
 
        if(@ftp_login($this->ftpobj,$this->user,$this->pwd)){
            ftp_pasv($this->ftpobj,$pasv);
 
        }else{
            require_once 'Lyc/Ftp/Exception.class.php';
            throw new Lyc_Ftp_Exception("FTP ERROR : Couldn't login to $this->host");
        }
    }
    /**
    * 上传文件
    *
    */
    public function upload($remotefile,$localfile){
 
    }
    /**
    * 下载文件
    *
    */
    public function download($localfile,$remotefile){
 
    }
    /**
    * 关闭连接
    *
    */
    public function close(){
        if(is_string($this->ftpobj)){
            ftp_close($this->ftpobj);
        }
    }
 
}
?>

 
 
二、LycFtpFtp.class.php   实现类

 代码如下 复制代码
 <?php
    /*  author:凹凸曼(lyc)
    /*  email: jar-c@163.com
    /*  time : 2011-04-22
    /*
    */
require_once 'Lyc/Ftp/Abstract.class.php';
class Lyc_Ftp_Ftp extends Lyc_Ftp_Abstract{
 
    public function __construct($host,$user,$pwd,$mode=FTP_BINARY,$port=21,$timeout=90,$pasv=TRUE){
        $this->host=$host;
        $this->user=$user;
        $this->pwd=$pwd;
        $this->mode=$mode;
        $this->port=$port;
        $this->timeout=$timeout;
        $this->pasv=$pasv;
        $this->init();
 
    }
    protected function init(){
 
            $this->connect();
            $this->login();
 
    }
    /**
    * 上传文件
    *
    */
    public function upload($remotefile,$localfile){
 
       $res=ftp_nb_put($this->ftpobj,$remotefile,$localfile,$this->mode,ftp_size($this->ftpobj,$remotefile));
       while($res==FTP_MOREDATA){
           $res=ftp_nb_continue($this->ftpobj);
       }
       if($res!=FTP_FINISHED){
           return FALSE;
       }
       return TRUE;
    }
    /**
    * 下载文件
    *
    */
    public function download($localfile,$remotefile){
        ftp_set_option($this->ftpobj,FTP_AUTOSEEK,FALSE);
        $res=ftp_nb_get($this->ftpobj,$localfile,$remotefile,$this->mode,ftp_size($this->ftpobj,$localfile));
        while($res==FTP_MOREDATA){
            $res=ftp_nb_continue($this->ftpobj);
        }
        if($res!=FTP_FINISHED){
            return FALSE;
        }
        return TRUE;
    }
}
?>

 
三、LycException.class.php  异常基类

 代码如下 复制代码
 <?php
    /*  author:凹凸曼(lyc)
    /*  email: jar-c@163.com
    /*  time : 2011-04-22
    /*  
 
    */
    class Lyc_Exception extends Exception{
 
    }
?>
 


四、LycFtpException.class.php  FTP异常类

 

 代码如下 复制代码
 <?php
    /*  author:凹凸曼(lyc)
    /*  email: jar-c@163.com
    /*  time : 2011-04-22
    */
require_once 'Lyc/Exception.class.php';
class Lyc_Ftp_Exception extends Lyc_Exception{
 
}
?>
 


五、测试区

 代码如下 复制代码


 <?php
   /**
    * 上传文件
    *
    */
    public  function uploadTest(){
        require_once 'Lyc/Ftp/Ftp.class.php';
        $host=23.64.41.13';     //主机
        $user='tguser';                //用户名
        $pwd="";                         //密码   端口默认21 也可改
        $ftp=new Lyc_Ftp_Ftp($host,$user,$pwd);
        $res=$ftp->upload('test.rar',"F:\wwwroot\testarea\Lyc\Test\test.rar");
        if(!$res){
            echo " upload failure";
        }
 
    }
 
    public function downloadTest(){
        require_once 'Lyc/Ftp/Ftp.class.php';
        $host=33.64.41.135';
        $user='tguser';
        $pwd="";
        $ftp=new Lyc_Ftp_Ftp($host,$user,$pwd);
        $res=$ftp->download("c:\test.rar","test.rar");
        if(!$res){
            echo "download failure";
        }
 
    }

上传文件HTML的输入标签FILE类型中的名称后要加[],作用是在HTML中向PHP建立数组,比如名称为pictures,多文件引用名称则为pictures[],实例如下:
 代码如下 复制代码

<form action=”upload.php” method=”post” enctype=”multipart/form-data”>
<p>
<input type=”file” name=”pictures[]” /><br />
<input type=”file” name=”pictures[]” /><br />
<input type=”file” name=”pictures[]” /><br />
<input type=”submit” value=”上传” />
</p>
</form> //手册中实例。

选择文件后点击上传

 代码如下 复制代码

<?php
print_r($_FILES);
?>

查看源文件:

 代码如下 复制代码
Array
(
[pictures] => Array
(
[name] => Array
(
[0] => file1.txt
[1] => file2.txt
[2] => file3.txt
)
[type] => Array
(
[0] => application/octet-stream
[1] => application/octet-stream
[2] => application/octet-stream
)
[tmp_name] => Array
(
[0] => D:EasyPHP\tmpphp47.tmp
[1] => D:EasyPHP\tmpphp48.tmp
[2] => D:EasyPHP\tmpphp49.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 94289
[1] => 65536
[2] => 102400
)
)
)

假设名为 /file1.txt??和 /file2.txt 的文件被提交,则 $_FILES['pictures']['name'][0] 的值将是 file1.txt,而 $_FILES['pictures']['name'][1] 的值将是 file2.txt。类似的,$_FILES['file2.txt']['size'][0] 将包含文件 file1.txt 的大小,

有了上面信息了我们要实现多文件上传就简单了

 代码如下 复制代码

<?php

class upload {
public $up_ext=array(), $up_max=5210, $up_dir;
private $up_name, $up_rename=true, $up_num=0, $up_files=array(), $up_ret=array();

function __construct($name, $ext=array(), $rename=true) {
if (!empty($name)) {
$this->up_name = $name;
!empty($ext) && $this->up_ext = $ext;
$this->up_rename = $rename;
$this->up_dir = website_dirroot.
$globals['cfg_upload_path'];
$this->initupload();
} else {
exit('upload文件域名称为空,初始化失败!');
}
}

private function initupload() {
if (is_array($_files[$this->up_name])) {
$up_arr = count($_files[$this->up_name]);
$up_all = count($_files[$this->up_name], 1);
$up_cnt = ($up_all - $up_arr) / $up_arr;
for ($i = 0; $i < $up_cnt; $i ++) {
if ($_files[$this->up_name]['error'][$i] != 4) {
$this->up_files[] = array(
'tmp_name' => $_files[$this->up_name]['tmp_name'][$i],
'name' => $_files[$this->up_name]['name'][$i],
'type' => $_files[$this->up_name]['type'][$i],
'size' => $_files[$this->up_name]['size'][$i],
'error' => $_files[$this->up_name]['error'][$i]
);
}
}
$this->up_num = count($this->up_files);
} else {
if (isset($_files[$this->up_name])) {
$this->up_files = array(
'tmp_name' => $_files[$this->up_name]['tmp_name'],
'name' => $_files[$this->up_name]['name'],
'type' => $_files[$this->up_name]['type'],
'size' => $_files[$this->up_name]['size'],
'error' => $_files[$this->up_name]['error']
);
$this->up_num = 1;
} else {
exit('没找找到需要upload的文件!');
}
}

$this->chkupload();
}

private function chkupload() {
if (empty($this->up_ext)) {
$up_mime = array('image/wbmp', 'image/bmp', 'image/gif', 'image/pjpeg', 'image/x-png');
foreach ($this->up_files as $up_file) {
$up_allw = false;
foreach ($up_mime as $mime) {
if ($up_file['type'] == $mime) {
$up_allw = true; break;
}
}
!$up_allw && exit('不允许上传'.$up_file['type'].'格式的文件!');

if ($up_file['size'] / 1024 > $this->up_max) {
exit('不允许上传大于 '.$this->up_max.'k 的文件!');
}
}
} else {
foreach ($this->up_files as $up_file) {
$up_ext = end(explode('.', $up_file['name']));

$up_allw = false;
foreach ($this->up_ext as $ext) {
if ($up_ext == $ext) {
$up_allw = true; break;
}
}
!$up_allw && exit('不允许上传.'.$up_ext.'格式的文件!');

if ($up_file['size'] / 1024 > $this->up_max) {
exit('不允许上传大于 '.$this->up_max.'k 的文件!');
}
}
}

$this->uploading();
}

private function uploading() {
if (io::dircreate($this->up_dir)) {
if (chmod($this->up_dir, 0777)) {
if (!empty($this->up_files)) {
foreach ($this->up_files as $up_file) {
if (is_uploaded_file($up_file['tmp_name'])) {
$file_name = $up_file['name'];
if ($this->up_rename) {
$file_ext = end(explode('.', $file_name));
$file_rnd = substr(md5(uniqid()), mt_rand(0, 26), 6);
$file_name = date('ymdhis').'_'.$file_rnd.'.'.$file_ext;
}
$file_name = $this->up_dir.'/'.$file_name;

if (move_uploaded_file($up_file['tmp_name'], $file_name)) {
$this->up_ret[] = str_replace(website_dirroot, '', $file_name);
} else {
exit('文件上传失败!');
}
}
}
}
} else {
exit('未开启写入权限!');
}
} else {
exit('上传目录创建失败!');
}
}

public function getupload() {
return empty($this->up_ret) ? false : $this->up_ret;
}

function __destruct() {}
}
?>

在上面我们会看到一个for ($i = 0; $i < $up_cnt; $i ++) ,这个是遍历我们上面讲的一个个实例了。

以前的php5.2之前的版本是不能可使用APC模块的,因为之前的压根就没有这个APC模块,如果想使用APC模块实现上传进度条我们必须是php5.2或更高版本。

从5.2开始APC加入了一个叫APC_UPLOAD_PROGRESS的东东,解决了困扰大家已久的进度条问题。并且它把原来的上传时把临时文件全部缓存到内存改成了当临时文件达到设定值时就自动保存到硬盘,有效地改善了内存利用状况。

它的作用原理是在上传时候赋予每个上传一个唯一的ID,当 PHP 脚本收到一个上传文件时,解释程序将自动检查 $_POST数组中名为 APC_UPLOAD_PROGRESS 的隐藏字段,它将成为缓存变量,存储关于上传的信息,这样脚本就可以通过上传的ID来访问上传文件的状态信息。

APC是Alternative PHP Cache的简称,是 PHP 的一个免费公开的优化代码缓存。它用来提供免费,公开并且强健的架构来缓存和优化 PHP 的中间代码。

APC模块的参数配置

 代码如下 复制代码

Name Default Changeable Changelog
apc.enabled 1 PHP_INI_ALL
apc.shm_segments 1 PHP_INI_SYSTEM
apc.shm_size 30 PHP_INI_SYSTEM
apc.optimization 0 PHP_INI_ALL
apc.num_files_hint 1000 PHP_INI_SYSTEM
apc.ttl 0 PHP_INI_SYSTEM
apc.gc_ttl 3600 PHP_INI_SYSTEM
apc.cache_by_default On PHP_INI_SYSTEM
apc.filters "" PHP_INI_SYSTEM
apc.mmap_file_mask "" PHP_INI_SYSTEM
apc.slam_defense 0 PHP_INI_SYSTEM
apc.file_update_protection 2 PHP_INI_SYSTEM
apc.enable_cli 0 PHP_INI_SYSTEM > APC 3.0.6

好了配置好了现在就开始写程序了

XML/HTML代码

 代码如下 复制代码

<!–以下为上传表单–>
<form enctype="multipart/form-data" id="upload_form" action="" method="POST">
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="upid"/>
视频标题:<input type="text" id="subject" name="subject"/>
视频说明:<input type="text" id="content" name="content"/>
视频TAG(以逗号分割)<input type="text" id="tag" name="tags"/>
<input type="file" id="upfile" name="upfile"/>
<input type="submit" id="filesubmit" value="上传" onclick="startProgress(‘upid’); return true;"/>
<!–注意:startProgress(‘upid’)中的参数是你从php中分配的唯一上传参数–>
</form>
<!–以下为上传进度条–>
<div id="upstatus" style="width: 500px; height: 30px; border: 1px solid ##ffffde; color:#796140;">
</div
<div id="progressouter" style="width: 500px; height: 20px; border: 3px solid #de7e00; display:none;">
<div id="progressinner" style="position: relative; height: 20px; color:#796140; background-color: #f6d095; width: 0%; "></div>
</div>

最主要的就是那个APC_UPLOAD_PROGRESS的隐藏域,有了它脚本才能去访问目前上传文件的状态,另外加一个显示上传状态的div就好了。

下面是处理Ajax的脚本,我用了Jquery框架,json传递消息。

JavaScript代码

 代码如下 复制代码


function getProgress(upid){
var url = "<{$siteurl}>epadmin/upprocess";
$.getJSON(
url,
{ progress_key: upid },
function(json){
$("#progressinner").width(json.per+"%");
$("#upstatus").html(‘文件大小:’+json.total+‘KB’+‘ 已上传:’+json.current+‘KB’);
if (json.per < 100){
setTimeout(function(){
getProgress(upid);
}, 10);
}else{
$("#upstatus").html("视频上传完成,正在处理数据,请稍后……");
}
}
)
}
function startProgress(upid){
$("#progressouter").css({ display:"block" });
setTimeout(function(){
getProgress(upid);
}, 100);
}

下来就是读取上传状态的PHP代码了,至于上传文件的处理可以按照平常自己的来写.

 

 代码如下 复制代码
//上传文件操作函数,可按照自己的需要编写
function upflvAction()
{
if($_SERVER['REQUEST_METHOD']==‘POST’){
$subject = trim($this->f->filter($this->_request->getPost(‘subject’)));
$content = trim($this->f->filter($this->_request->getPost(‘content’)));
Zend_Loader::loadClass(‘Custom_FlvOp’);
$flv = new Custom_FlvOp;
$flv->uploadFlv(‘upfile’,$subject,$content);
}
}
//这就是读取上传状态的函数了~~
function upprocessAction()

if(isset($_GET['progress_key'])) {
$status = apc_fetch(‘upload_’.$_GET['progress_key']);
$json = array(
‘per’=>$status['current']/$status['total']*100,
‘total’=>round($status['total']/1024),
‘current’=>round($status['current']/1024),
);
require_once("Zend/Json.php");
echo Zend_Json::encode($json);
}
}

一些关于apc配置详解

apc.enabled  布尔型
apc.enabled 可以被设成 0 来禁用 APC。这主要是有用的,当 APC 被静态编译入 PHP 时,因为没有其它方法来禁用它(当编译为 DSO 的时候,可以将 php.ini 中的 extension 行注释掉)。

apc.shm_segments  整型
对编译缓存分配共享内存块的数量。如果APC用光了共享内存,而且你已经设置 apc.shm_size为系统允许的最大值的情况下,你可以试着去提高这个参数的值。

apc.shm_size  整型
每个共享内存块的大小是以MB为单位的。在默认情况下,一些系统(包括大多数BSD变种系统)的共享内存块的大小限制的很低。

apc.optimization  整型
优化等级。设为0则禁用优化,越高的值使用越强有力的优化。期待有适度的速度上的改进。这个还是实验性质的。

apc.num_files_hint  整型
对在你的Web服务器上被包含和请求的不同的源文件的数量的提示。如果你无法确定,设置为0或者省略;这个设置主要可能用于有成千的源文件的站点。

apc.ttl  整型
当一个缓存条目在缓存区的位置被另一个条目需要时,我们需要考虑的是这个缓存条目在缓存区的位置被允许空闲的秒数。将这个参数设置为0意味着你的缓存可能充满不新鲜的条目,同时导致新的条目无法被缓存。

apc.gc_ttl  整型
缓存条目在垃圾收集列表中存活的秒数。这个值提供了出错保护在执行一个缓存源文件,而同时服务器进程死了的事件中。如果那个源文件被修改,内存分配给旧版本的缓存条目将不会被回收,直到这个参数设定的TTL值到的时候。设置为0就是禁止这个特性。

apc.cache_by_default  布尔型
默认为On,但可以被设置为Off并和以加号开头的apc.filters配合使用,文件仅仅在匹配过滤器时才被缓存。

apc.filters  字符串
一个以逗号分割的POSIX扩展正则表达式的列表。如果任何模式匹配源文件名,这个文件将不会被缓存。注意用来匹配的文件名是传递给 include/require 的文件名,而不是绝对路径。如果正则表达式的第一个字符是 + ,则这个表达式就意味着任何匹配表达式的文件将会被缓存,如果第一个字符是 - 则任何匹配都不会被缓存。 - 是默认值,所以可以被省略。

apc.mmap_file_mask  字符串


apc.slam_defense  整型
在非常繁忙的服务器上,无论你启动服务还是修改文件,你都会导致一种多进程都试图在同一个时间缓存同一个文件的竞争。这个选项设置了进程跳过试图去缓存一个未被缓存的文件的百分比。或者可以把这个想象成一个单独进程跳过缓存的机率。例如,设置apc.slam_defense为75就意味着进程有75%的机率不去缓存未被缓存的文件。所以,设置的越高,越能减少缓存的碰撞机率。设置为0则禁用这个特性。

apc.file_update_protection  整型
当你在一个运行着的服务器上修改文件时,你应该执行原子操作。也就是,先写一个临时文件,当写完后再重命名(mv)这个文件到它的最终位置。许多文本编辑器,cp,tar和其他一些类似程序都不是这样操作的。这就意味着有机会去访问和(缓存)文件,当这个文件还在被写的情况下。apc.file_update_protection的设置使得缓存标记新文件的延迟。默认值是2,意味着如果发现文件的修改时间距离访问时间不到2秒,文件将不会被缓存。访问写到一半的文件的不幸用户将会看到离奇的情况,但至少这种情况不是持续的。如果你确信你经常使用原子操作来更新你的文件,你可以关闭这个保护通过设置这个参数为0。如果你的系统充满io操作,并导致更新程序花费超过2秒,你可能需要去增大这个值。

apc.enable-cli  整型
大多是为了测试和调试。为CLI版本的PHP开启动APC功能。一般来说,你将不会想到为每一个 CLI请求创建,移植和放弃APC的缓存,但对于各种测试情况,这是很容易的为了CLI版本开启APC。

一个利用数组来实现无限级分类php类程序,有需要的朋友可参考,这里就不多说什么了直接复制上代码。
 代码如下 复制代码


<?php
$cates = array(
 array(
  'cid'   => 1,
  'cname' => '新闻',
  'pid'   => 0
 ),

 array(
  'cid'   => 2,
  'cname' => '通知',
  'pid'   => 0
 ),

 array(
  'cid'   => 3,
  'cname' => '国内新闻',
  'pid'   => 1
 ),

 array(
  'cid'   => 4,
  'cname' => '国际新闻',
  'pid'   => 1
 ),

 array(
  'cid'   => 5,
  'cname' => '北京新闻',
  'pid'   => 3
 ),

 array(
  'cid'   => 6,
  'cname' => '上海新闻',
  'pid'   => 3
 ),

 array(
  'cid'   => 7,
  'cname' => '紧急通知',
  'pid'   => 2
 ),

 array(
  'cid'   => 8,
  'cname' => '一般通知',
  'pid'   => 2
 ),
);

/**
 * 生成菜单
 *
 * @param array $data 原始数据
 * @param integer $pid 当前分类的父id
 * @return array 处理后数据
 */
function createMenuTree($data = array(), $pid = 0)
{
 if (empty($data))
 {
  return array();
 }

 static $level = 0;

 $returnArray = array();

 foreach ($data as $node)
 {
  if ($node['pid'] == $pid)
  {
   $returnArray[] = array(
    'cid'   => $node['cid'],
    'cname' => $node['cname'],
    'level' => $level
   );

   if (hasChild($node['cid'], $data))
   {
    $level++;

    $returnArray = array_merge($returnArray, createMenuTree($data, $node['cid']));

    $level--;
   }
  }
 }

 return $returnArray;
}

/**
 * 检查是否有子分类
 *
 * @param integer $cid 当前分类的id
 * @param array $data 原始数据
 * @return boolean 是否有子分类
 */
function hasChild($cid, $data)
{
 $hasChild = false;

 foreach ($data as $node)
 {
  if ($node['pid'] == $cid)
  {
   $hasChild = true;
   break;
  }
 }

 return $hasChild;
}

header('Content-Type: text/html; charset=utf-8');

$result = createMenuTree($cates);

foreach ($result as $row)
{
 for ($i = 0; $i < $row['level']; $i++)
 {
  echo "t";
 }

 echo $row['cname'] . "n";
}
?>

[!--infotagslink--]

相关文章