QeePHP中的优秀函数(二)

这两个函数来自于Helper_Array,我觉得是非常常用的方法,功能也比较强大。适合大家使用。

    /**
     * 将一个平面的二维数组按照指定的字段转换为树状结构
     *
     * 用法:
     * @code php
     * $rows = array(
     *     array('id' => 1, 'value' => '1-1', 'parent' => 0),
     *     array('id' => 2, 'value' => '2-1', 'parent' => 0),
     *     array('id' => 3, 'value' => '3-1', 'parent' => 0),
     *
     *     array('id' => 7, 'value' => '2-1-1', 'parent' => 2),
     *     array('id' => 8, 'value' => '2-1-2', 'parent' => 2),
     *     array('id' => 9, 'value' => '3-1-1', 'parent' => 3),
     *     array('id' => 10, 'value' => '3-1-1-1', 'parent' => 9),
     * );
     *
     * $tree = Helper_Array::tree($rows, 'id', 'parent', 'nodes');
     *
     * dump($tree);
     *   // 输出结果为:
     *   // array(
     *   //   array('id' => 1, ..., 'nodes' => array()),
     *   //   array('id' => 2, ..., 'nodes' => array(
     *   //        array(..., 'parent' => 2, 'nodes' => array()),
     *   //        array(..., 'parent' => 2, 'nodes' => array()),
     *   //   ),
     *   //   array('id' => 3, ..., 'nodes' => array(
     *   //        array('id' => 9, ..., 'parent' => 3, 'nodes' => array(
     *   //             array(..., , 'parent' => 9, 'nodes' => array(),
     *   //        ),
     *   //   ),
     *   // )
     * @endcode
     *
     * 如果要获得任意节点为根的子树,可以使用 $refs 参数:
     * @code php
     * $refs = null;
     * $tree = Helper_Array::tree($rows, 'id', 'parent', 'nodes', $refs);
     *
     * // 输出 id 为 3 的节点及其所有子节点
     * $id = 3;
     * dump($refs[$id]);
     * @endcode
     *
     * @param array $arr 数据源
     * @param string $key_node_id 节点ID字段名
     * @param string $key_parent_id 节点父ID字段名
     * @param string $key_childrens 保存子节点的字段名
     * @param boolean $refs 是否在返回结果中包含节点引用
     *
     * return array 树形结构的数组
     */
    static function toTree($arr, $key_node_id, $key_parent_id = 'parent_id',
                           $key_childrens = 'childrens', & $refs = null)
    {
        $refs = array();
        foreach ($arr as $offset => $row)
        {
            $arr[$offset][$key_childrens] = array();
            $refs[$row[$key_node_id]] =& $arr[$offset];
        }

        $tree = array();
        foreach ($arr as $offset => $row)
        {
            $parent_id = $row[$key_parent_id];
            if ($parent_id)
            {
                if (!isset($refs[$parent_id]))
                {
                    $tree[] =& $arr[$offset];
                    continue;
                }
                $parent =& $refs[$parent_id];
                $parent[$key_childrens][] =& $arr[$offset];
            }
            else
            {
                $tree[] =& $arr[$offset];
            }
        }

        return $tree;
    }

    /**
     * 将树形数组展开为平面的数组
     *
     * 这个方法是 tree() 方法的逆向操作。
     *
     * @param array $tree 树形数组
     * @param string $key_childrens 包含子节点的键名
     *
     * @return array 展开后的数组
     */
    static function treeToArray($tree, $key_childrens = 'childrens')
    {
        $ret = array();
        if (isset($tree[$key_childrens]) && is_array($tree[$key_childrens]))
        {
            $childrens = $tree[$key_childrens];
            unset($tree[$key_childrens]);
            $ret[] = $tree;
            foreach ($childrens as $node)
            {
                $ret = array_merge($ret, self::treeToArray($node, $key_childrens));
            }
        }
        else
        {
            unset($tree[$key_childrens]);
            $ret[] = $tree;
        }
        return $ret;
    }

不过显而易见,这两个函数,都不需要多介绍,tree2list,list2tree,想想也知道怎么用,再加上注释又比较全。
可惜QeePHP不再开发,而ThinkPHP积下来的问题又很多,改动起来也非常痛苦。所以我开始慢慢分析一下。

QeePHP中的优秀函数(一)

基类Q中的normalize。

    /**
     * 对字符串或数组进行格式化,返回格式化后的数组
     *
     * $input 参数如果是字符串,则首先以“,”为分隔符,将字符串转换为一个数组。
     * 接下来对数组中每一个项目使用 trim() 方法去掉首尾的空白字符。最后过滤掉空字符串项目。
     *
     * 该方法的主要用途是将诸如:“item1, item2, item3” 这样的字符串转换为数组。
     *
     * @code php
     * $input = 'item1, item2, item3';
     * $output = Q::normalize($input);
     * // $output 现在是一个数组,结果如下:
     * // $output = array(
     * //   'item1',
     * //   'item2',
     * //   'item3',
     * // );
     *
     * $input = 'item1|item2|item3';
     * // 指定使用什么字符作为分割符
     * $output = Q::normalize($input, '|');
     * @endcode
     *
     * @param array|string $input 要格式化的字符串或数组
     * @param string $delimiter 按照什么字符进行分割
     *
     * @return array 格式化结果
     */
    static function normalize($input, $delimiter = ',')
    {
        if (!is_array($input))
        {
            $input = explode($delimiter, $input);
        }
        $input = array_map('trim', $input);
        return array_filter($input, 'strlen');
    }

看到这个方法,其实应该感觉得到它的用户,确实,大多数情况下,我们都是用explode来分割字符串的,但事实上,你不能保证别人给你的字符串有没有多余的空格,或者无效的字符串,因此,通过这个函数,就可以去除掉很无效的值 。建议使用它来代替explode,但,如果确实是很规则的,还是用explode吧,毕竟,如果这个分割出来的数组很大,效率肯定比explode低很多了。

采用 CURL登录量子统计(二)

本文就全是代码了。。。

$cookiefile = tempnam( './log/' , 'cookie' );//设定cookie文件的路径 。
$ch = curl_init();
$header[]="Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_URL, 'http://www.linezing.com/login.php');  //登录地址
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //发送header ,其实这个header可以不发送
curl_setopt($ch, CURLOPT_POST, 1);  //这是POST数据
curl_setopt($ch, CURLOPT_POSTFIELDS, 'referer=&webname=index&username=用户名&password=密码&submit=%E7%99%BB%E5%BD%95');//http_build_query( $postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //这个是代表curl_exec后取返回成字符串,而不是象WEB一样跳转
curl_setopt($ch, CURLOPT_HEADER, 0);  //curl返回的时候,默认都是带有header信息的,所以这里设为0,代表返回的时候不要header信息
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); //这是在用sniff抓包的时候发现用了gzip,deflate的encoding,
curl_setopt($ch, CURLOPT_REFERER, 'http://www.linezing.com/');//记录来源的Referer
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookiefile);
curl_exec($ch);  //我这里并没有取返回值,主要是把cookie记录下来

curl_setopt($ch, CURLOPT_URL, 'http://www.linezing.com/router.php');  //登录后跳转的网址
//curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile); 原先我在这里也记录cookie了,但事实上,我这样做之后,反而会把第一次登录时的cookie覆盖了。。。郁闷
//curl_setopt($ch, CURLOPT_COOKIEJAR,$cookiefile);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.linezing.com/');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION,1);
$res = curl_exec($ch);  

curl_setopt($ch, CURLOPT_URL, 'http://tongji.linezing.com/welcome.html');  //welcome页还会再判断是否登录,如果没有登录,会是一段JS跳到www.linezing.com
//curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile);
//curl_setopt($ch, CURLOPT_COOKIEJAR,$cookiefile);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.linezing.com/');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION,1);
curl_exec($ch);  

curl_setopt($ch, CURLOPT_URL, 'http://tongji.linezing.com/mystat.html');
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookiefile);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION,1);
$res = curl_exec($ch);  //这里获取返回值,我是想看一下是不是正确。。。
curl_close($ch);
echo htmlSpecialChars( $res );

因为是临时写的代码,所以没有封装,但也算是尽量做了注释。。

采用CURL登录量子统计(一)

我自己的站点用的是量子统计,所以对于量子统计就需要有更多的研究和学习。
我想的是,如果我能够通过手机,每天在自己的某个指定页面,就能够看到朋友们过来时的关键字或者搜索引擎关键字等。我就可以利用他们有针对性的对网站进行优化?也可以随时关注网站的流量等信息了。

于是,想到什么就做什么,我直接用snoopy类开始尝试提交post数据,提交后取后返回值 ,并同时打开http://tongji.linezing.com/mystat.html页面(这是一个汇总页),但结果都是提示我需要登录。

如此反复尝试多次,也尝试用curl进行登录,但都是一直失败。最后我不得不祭起抓包利器:smartsniff,这是一个小工具,但是他的抓包功能很强。于是我对我的行为开始抓包,从登录直到显示mystat.html页面,结果却发现,从登录开始,到显示mystat.html页面,一共抓了四次包,他们分别是:

  1. http://www.linezing.com/login.php 登录提交页,POST提交
  2. http://www.linezing.com/router.php GET方式
  3. http://tongji.linezing.com/welcome.html GET方式
  4. http://tongji.linezing.com/mystat.html GET方式

其实我觉得奇怪的是,这四个页面是在同一台机器上,而且主机名却是bbs.linezing.com,好妖异。

不过,既然分析了抓包数据,得到这四个页面,那么剩下的就开始写代码了,欲知后事如何,请看周一的代码分析(其实是因为代码在单位的电脑上,在家里地无法更新而己),敬请关注。