百度 Google Ping主动推送方法

 更新时间:2016年11月25日 15:30  点击:1472
主动推送就是发布文章成功之后告诉搜索引擎来抓取了,这个百度自带了例子,下面是在这个基本上进行改进的哦.
function ping($data){

    $server_url = array(

        'baidu' => 'http://ping.baidu.com/ping/RPC2',

       // 'google' => 'http://blogsearch.google.com/ping/RPC2',

        //'bloglines' => 'http://www.bloglines.com/ping'

    );

 

    $xml = '

       

       
weblogUpdates.extendedPing

       

       
' . $data['blog_name'] . '

       
' . $data['blog_home'] . '

       
' . $data['blog_url'] . '

       
' . $data['rss_url'] . '

       

       
'

 

    $xml_baidu = '

       

       
weblogUpdates.extendedPing

       

       
' . $data['blog_name'] . '

       
' . $data['blog_home'] . '

       
' . $data['blog_url'] . '

       
' . $data['rss_url'] . '

       

       
'

 

    
foreach
($server_url as $k => $s){

        $s = trim($s);

        if(!empty($s)){

            $c = curl_init();

            $header = array(

                "Content-type: text/xml;charset=\"utf-8\"",

                "Accept: text/xml"

            );

            
curl_setopt
_array(

                $c,

                array(

                    CURLOPT_URL => $s,

                    CURLOPT_RETURNTRANSFER => true,

                    CURLOPT_POST => true,

                    CURLOPT_HTTPHEADER => $header,

                    CURLOPT_POSTFIELDS => $k === 'baidu' ? $xml_baidu : $xml

                )

            );

            $content=curl_exec($c);

            var_dump($content);

            curl_close($c);

        }

    }

 

}

下面我们来看一篇关于dwz增删改查不局部刷新,希望这篇文章能够帮助到各位朋友,具体的细节步骤如下文介绍.

一个小项目,用到了dwz,苦于官方少于维护且需要一部分前端技术一直没有深挖,迫在眉睫,遇到了跟很多人一样的问题,就是增删改查不能局部刷新,苦恼啊。经多多放查找资料和翻阅源码终于皇天不负有心。

项目使用TP3.1.3

需要做一下修改

找到 Thinkphp/lib/core/Action.class.php

查找 ajaxReturn 方法

注释掉

if(func_num_args()>2) {// 兼容3.0之前用法
            $args           =   func_get_args();
            array_shift($args);
            $info           =   array();
            $info['data']   =   $data;
            $info['info']   =   array_shift($args);
            $info['status'] =   array_shift($args);
            $data           =   $info;
            $type           =   $args?array_shift($args):'';
}


然后在下方添加

if(method_exists($this,"ajaxAssign")) $this->ajaxAssign($data);//ajax赋值扩展


完整代码如下

/**
     * Ajax方式返回数据到客户端
     * @access protected
     * @param mixed $data 要返回的数据
     * @param String $type AJAX返回数据格式
     * @return void
     */
    protected function ajaxReturn($data,$type='') {
//        if(func_num_args()>2) {// 兼容3.0之前用法
//            $args           =   func_get_args();
//            array_shift($args);
//            $info           =   array();
//            $info['data']   =   $data;
//            $info['info']   =   array_shift($args);
//            $info['status'] =   array_shift($args);
//            $data           =   $info;
//            $type           =   $args?array_shift($args):'';
//        }
        if(method_exists($this,"ajaxAssign")) $this->ajaxAssign($data);//ajax赋值扩展
        if(empty($type)) $type  =   C('DEFAULT_AJAX_RETURN');
        switch (strtoupper($type)){
            case 'JSON' :
                // 返回JSON数据格式到客户端 包含状态信息
                //header('Content-Type:application/json; charset=utf-8');
                 header('Content-Type:text/html; charset=utf-8'); //zhanghuihua
                exit(json_encode($data));
            case 'XML'  :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($data));
            case 'JSONP':
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                $handler  =   isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
                exit($handler.'('.json_encode($data).');'); 
            case 'EVAL' :
                // 返回可执行的js脚本
                header('Content-Type:text/html; charset=utf-8');
                exit($data);           
            default     :
                // 用于扩展其他返回格式数据
                tag('ajax_return',$data);
        }
}

在任意一出添加

protected function ajaxAssign(&$result){
        $result['statusCode']  =  $result['status'];
        $result['navTabId']  =  $_REQUEST['navTabId'];
        $result['message']=$result['info'];
}


退出DWZ,重新登录 再试试吧!

egister_shutdown_function 函数是什么函数呢,我们一起来看看关于egister_shutdown_function 用法吧,具体的细节步骤如下.

设定错误和异常处理三函数

register_shutdown_function(array(‘Debug’,'fatalError’)); //定义PHP程序执行完成后执行的函数

set_error_handler(array(‘Debug’,'appError’)); // 设置一个用户定义的错误处理函数

set_exception_handler(array(‘Debug’,'appException’)); //自定义异常处理。

下面讲一下register_shutdown_function() 函数.

 

功能:register_shutdown_function() 函数可实现当程序执行完成后执行的函数,其功能为可实现程序执行完成的后续操作。程序在运行的时候可能存在执行超时,或强制关闭等情况,但这种情况下默认的提示是非常不友好的,如果使用register_shutdown_function()函数捕获异常,就能提供更加友好的错误展示方式,同时可以实现一些功能的后续操作,如执行完成后的临时数据清理,包括临时文件等。

可以这样理解调用条件:

1、当页面被用户强制停止时

2、当程序代码运行超时时

3、当PHP代码执行完成时,代码执行存在异常和错误、警告

实例说明

1. 一个简介的例子 , 调用一个函数

<?php

function test() {

    echo "test()";

}

register_shutdown_function(array("test"));

echo "show: ";

?>

 

 

这个例子输出 

 show: test()
这个函数的作用就是在退出脚本前,调用已经注册的函数,并执行该函数。

 

2. 在类中用一个数组作为一个参数

<?php

    class ClassDemo {

        public function __construct() {

            register_shutdown_function(array($this, "f"));

        }

 

        public function f() {

            echo "f()";

        }

    }

 

    $demo = new ClassDemo();

    echo "before </br>";

?>

输出:

before

f()

 

3. 带有参数的例子:

<?php

function f($str) {

        echo $str."<br>";

}

 

register_shutdown_function("f","hello");

 

    class ClassDemo {

        public function __construct() {

            register_shutdown_function(array($this, "f"),"hello");

        }

 

        public function f($str) {

            echo "f():".$str;

        }

    }

 

   $demo = new ClassDemo();

   echo "before </br>";  

?>

 

注意事项

1,register_shutdown_function()函数可重复调用,但执行的顺序与注册的顺序相同
2,如果在调用register_shutdown_function()函数之前有exit()函数调用,register_shutdown_function()函数将不能执行
3,PHP4后支持注册函数参数传递
4,在某些服务端,如Apache,当前目录在register_shutdown_function()函数中能够改变
5,register_shutdown_function()函数执行在headers发送之后

下面我们来看一篇关于php向一个字符串随机位置插入一个字符串的例子,这个和固定有有一些区别了,具体的如下.

/**
* 向一个字符串随机插入一个字符串
* @param unknown_type $oldstr    老字符串
* @param unknown_type $instr    插入的字符串
* @param unknown_type $encoding    字符串编码
*/
function rand_in_str($oldstr,$instr,$encoding){
$len = mb_strlen($oldstr,$encoding);
$insert_point = mt_rand(1,$len-1);
$pre_str = mb_substr($oldstr,0,$insert_point,$encoding);
$after_str = mb_substr($oldstr,$insert_point,$len-$insert_point,$encoding);
$newstr = $pre_str.$instr.$after_str;
return $newstr;
}

Opcode缓存可以给php加速了,我们这里来看一篇关于PHP Opcode缓存加速组件:APC详解介绍吧,希望文章能够给各位带来帮助.

PHP在性能上相对于其他编译型语言来说算不上突出,但是使用了Opcode缓存后性能提升还是很明显的。常见的缓存加速组件主要有 eAccelerator,XCache,APC本文主要介绍APC的安装使用。

APC,全称是Alternative PHP Cache,官方翻译叫"可选PHP缓存"。它为我们提供了缓存和优化PHP的中间代码的框架。 APC的缓存分两部分:系统缓存和用户数据缓存。

系统缓存

它是指APC把PHP文件源码的编译结果缓存起来,然后在每次调用时先对比时间标记。如果未过期,则使用缓存的中间代码运行。默认缓存3600s(一小时)。但是这样仍会浪费大量CPU时间。因此可以在php.ini中设置system缓存为永不过期(apc.ttl=0)。不过如果这样设置,改运php代码后需要重启WEB服务器。目前使用较多的是指此类缓存。

用户数据缓存

缓存由用户在编写PHP代码时用apc_store和apc_fetch函数操作读取、写入的。如果数据量不大的话,可以一试。如果数据量大,使用类似memcache此类的更加专著的内存缓存方案会更好。

APC模块的安装

最简单的方法是直接使用pecl,在命令行下输入:/usr/local/php/bin/pecl install apc
然后按照提示一步步完成即可,示例如下:

[root@iZ23bm1tc0pZ ~]# /usr/local/php/bin/pecl install apc
downloading APC-3.1.13.tgz ...
Starting to download APC-3.1.13.tgz (171,591 bytes)
.....................................done: 171,591 bytes
55 source files, building
running: phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
Enable internal debugging in APC [no] : no
Enable per request file info about files used from the APC cache [no] : no
Enable spin locks (EXPERIMENTAL) [no] : no
Enable memory protection (EXPERIMENTAL) [no] : no
Enable pthread mutexes (default) [no] : no
Enable pthread read/write locks (EXPERIMENTAL) [yes] : yes

然后重启服务器即可:

lnmp nginx restart

先看一下没有使用apc情况下的压测结果:

[root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 http://zfsphp.cn/index.php
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking zfsphp.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:        nginx
Server Hostname:        zfsphp.cn
Server Port:            80
Document Path:          /index.php
Document Length:        14341 bytes
Concurrency Level:      100
Time taken for tests:   15.517 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      14544000 bytes
HTML transferred:       14341000 bytes
Requests per second:    64.45 [#/sec] (mean)
Time per request:       1551.671 [ms] (mean)
Time per request:       15.517 [ms] (mean, across all concurrent requests)
Transfer rate:          915.34 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   4.8      0      17
Processing:    46 1481 277.0   1560    1638
Waiting:       42 1481 277.1   1560    1638
Total:         58 1482 272.8   1560    1638
Percentage of the requests served within a certain time (ms)
  50%   1560
  66%   1576
  75%   1582
  80%   1587
  90%   1602
  95%   1612
  98%   1622
  99%   1629
 100%   1638 (longest request)
可见最大吞吐率只有64.45reqs/s

然后我们开启apc,测试结果如下:

[root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 http://zfsphp.cn/index.php
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking zfsphp.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:        nginx
Server Hostname:        zfsphp.cn
Server Port:            80
Document Path:          /index.php
Document Length:        14341 bytes
Concurrency Level:      100
Time taken for tests:   7.122 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      14544000 bytes
HTML transferred:       14341000 bytes
Requests per second:    140.41 [#/sec] (mean)
Time per request:       712.189 [ms] (mean)
Time per request:       7.122 [ms] (mean, across all concurrent requests)
Transfer rate:          1994.29 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.4      0      10
Processing:    23  677 125.3    705     775
Waiting:       22  677 125.4    705     775
Total:         30  678 123.1    705     775
Percentage of the requests served within a certain time (ms)
  50%    705
  66%    719
  75%    726
  80%    730
  90%    742
  95%    750
  98%    760
  99%    765
 100%    775 (longest request)

 可见吞吐率提高了一倍多,达到140.41reqs/s。
 然后,我们在开启动态内容缓存(楼主的博客用的是Smarty缓存),测试结果如下:

 [root@iZ23bm1tc0pZ ~]# ab -n1000 -c100 http://zfsphp.cn/index.php
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking zfsphp.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:        nginx
Server Hostname:        zfsphp.cn
Server Port:            80
Document Path:          /index.php
Document Length:        14341 bytes
Concurrency Level:      100
Time taken for tests:   2.263 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      14544000 bytes
HTML transferred:       14341000 bytes
Requests per second:    441.98 [#/sec] (mean)
Time per request:       226.255 [ms] (mean)
Time per request:       2.263 [ms] (mean, across all concurrent requests)
Transfer rate:          6277.49 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   3.1      0      12
Processing:    18  215  38.1    222     255
Waiting:       18  215  38.3    222     255
Total:         26  216  35.6    223     255
Percentage of the requests served within a certain time (ms)
  50%    223
  66%    230
  75%    232
  80%    234
  90%    237
  95%    239
  98%    240
  99%    243
 100%    255 (longest request)

这一次吞吐率居然达到441.98reqs/s,提高了三倍多,相比最初的64.45reqs/s提高了近7倍,可见使用apc的opcode缓存配合Smarty缓存,对网站性能的优化效果还是相当明显的。

[!--infotagslink--]

相关文章

  • 百度网盟和google网盟推广那种效果好

    专做了百度和google的网盟推广以作推广效果的评估比较。百度的周期为6天,google为4天。   从百度的统计数据可以看出这六天的点击次数总共为464,平均点击花费了0.30元...2017-07-06
  • 解决idea2020.1 用gitee push推送被拒绝的原因(亲测有效)

    这篇文章主要介绍了解决idea2020.1 用gitee push推送被拒绝的原因(亲测有效),本文给大家分享解决方案,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-27
  • 如何根据百度地图计算出两地之间的驾驶距离(两种语言js和C#)

    以下是使用js代码实现百度地图计算两地距离,代码如下所示:<script src="js/jquery-1.9.0.js" type="text/javascript" language="javascript"></script><script language="javascript" type="text/javascript" src="js/...2015-10-30
  • 百度联盟封号了解封申请攻略

    百度联盟封号对于许多的站长来说肯定是会影响到心情的,那么既然是百度联盟封号了我们就肯定有一些原因的,虽然不是你自己搞的或一些其它因素都有可能,我们下面整理一下百...2016-10-10
  • 基于JavaScript实现高德地图和百度地图提取行政区边界经纬度坐标

    本文给大家介绍javascript实现高德地图和百度地图提取行政区边界经纬度坐标的相关知识,本文实用性非常高,代码简单易懂,需要的朋友参考下吧...2016-01-24
  • 网站被百度拔毛 一月重新回归经验分离

    网站被K后,笔者做的第一件事便是在网站上增加更新模块。百度算法更新,对于网站内容给予了相当大的权重,这是笔者网站最欠缺的部分,保证了及时的更新便有了吸引蜘蛛爬取的...2016-10-10
  • 如何用PS将百度熊抱枕做成大头枕?

    我们知道百度熊抱枕很大,只想要百度熊的大头,该怎么办呢?下面我们就来看看详细的教程。 1、首先打开photoshopCS6软件,新建一个“1600*1600”的白色背景图,并从右上角...2016-12-31
  • 如何根据百度地图计算出两地之间的驾驶距离(两种语言js和C#)

    以下是使用js代码实现百度地图计算两地距离,代码如下所示:<script src="js/jquery-1.9.0.js" type="text/javascript" language="javascript"></script><script language="javascript" type="text/javascript" src="js/...2015-10-30
  • 怎么对百度网页搜索的检索指标进行评估

    用户为满足自己某种需求而来到搜索引擎的,判断用户的需求是做好网站的开始。只有准确地判断出用户需求,了解用户搜索的目的,才能合理地衡量出一个网站结果的质量好,做好搜...2016-10-10
  • 如何应对百度更新改动关键词

    网页头部head 部分:TITLE 、<mtea>部分 网站描述Description、keyword 内容中的关键词,大家优化时也都比较重视这部分的关键词优化,因为在查询搜索结果时此处出现的关键词...2017-07-06
  • Google会不会取消PR的理由分析

    Google是这样介绍PageRank的:   Google 出类拔萃的地方在于专注开发“完美的搜索引擎”,联合创始人拉里&middot;佩奇将这种搜索引擎定义为可“确解用户...2017-07-06
  • JS实现获取来自百度,Google,soso,sogou关键词的方法

    这篇文章主要介绍了JS实现获取来自百度,Google,soso,sogou关键词的方法,结合实例形式分析了js获取来路页面的方法与相关搜索引擎关键词的处理技巧,需要的朋友可以参考下...2017-01-09
  • WinForm调用百度地图接口用法示例

    这篇文章主要介绍了WinForm调用百度地图接口用法,结合具体实例形式简单分析了WinForm WebBrower控件与前端百度接口交互的相关操作技巧,需要的朋友可以参考下...2020-06-25
  • Illustrator绘制百度云标志LOGO教程

    今天小编在这里就来给Illustrator的这一款软件的使用者们来说一说绘制百度云标志LOGO的教程,各位想知道具体绘制方法的使用者,那么下面就快来跟着小编一起看一看教程。...2016-09-14
  • 百度自动推送代码JS自动推送进化版

    百度站长平台提供链接索引的自动提交 JS 代码脚本。用百度自己的话讲:JS链接推送代码以网页为最小对象,服务于全平台多终端,PC站和移动站均可使用。...2020-06-03
  • PHPCMS实现自动推送URL到百度站长平台

    我们一起来看一篇关于PHPCMS实现自动推送URL到百度站长平台,希望此教程能够帮助到各位朋友。 百度站长平台开放url推送接口,可以使用调用接口的形式主动及时推送u...2016-11-25
  • 百度指数工具分析关键词的价值

    仅仅凭自己的感觉是根本无法判断这个关键词的价值的,那么这时候我们就可以适当的应用一下百度指数工具,利用这个工具我们可以挖掘出一些有价值的关键词。 一、从百...2016-10-10
  • C# 10分钟完成百度人脸识别(入门篇)

    这篇文章主要介绍了C# 10分钟完成百度人脸识别(入门篇),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 百度不更新网站原因分析

    百度不更新网站原因分析 今天我们来看一篇关于 哦,你是不是其它的之一呢,好了下面来看看各位站长总结了来百度不更新网页的原因吧。 一、首页的大flash图片。   ...2016-10-10
  • 网站的聚合页或站内搜索页怎么会沦为百度搜索打击对象

    网站运营是很多人向往的赚钱方法,一些网站会发现,自己做的聚合页面和站内搜索页面被百度搜索无情打击,这是为什么,不知道原因的伙伴下面跟小编一起来看看。 今天杨子...2017-07-06