You must say:"Yes" or "All right"! 注册 | 登陆
浏览模式: 标准 | 列表2009年03月的文章

说说我博客在搜索引擎中的权重

今天上午发布了一篇文章,就是关于PHP 读取 XML 类《PHP XML Library, version 1.2b》这一篇,下午吃饭后,继续使用搜索引擎搜索有关PHP与XML相关的内容,结果惊奇的发现百度居然在半个小时内收录了我的博客文章,而且排名不错,已经排到百度第一页第四条了,强烈B4一下,嘿嘿,完事,看看这篇文章收录需要几分钟呢!2009年3月31日18:33分发布!
一小时后:

图片附件(缩略图):
大小: 36.32 K
尺寸: 500 x 135
浏览: 34 次
点击打开新窗口浏览全图

Tags: 博客

PHP 读取 XML 类《PHP XML Library, version 1.2b》

昨天申请了阿里妈妈淘客广告的API,客服告诉我需要两三天的时间才可以审核下来,不知道运气如何了,呵呵,API一般都是XML传递数据来的,所以先发一个比较好用的PHP XML读取类。

今天晚上更新了本机的PHP环境配置,刚才去下载APMServ的时候,发现张宴也推荐了这个PHP读取XML的类,不过他写得更详细,还有使用方法来的,如果不知道怎么样使用的朋友可以参考参考:http://blog.s135.com/post/253/

PHP代码
  1. <?php   
  2. ###################################################################################   
  3. #   
  4. # XML Library, by Keith Devens, version 1.2b   
  5. # http://keithdevens.com/software/phpxml   
  6. #   
  7. # This code is Open Source, released under terms similar to the Artistic License.   
  8. # Read the license at http://keithdevens.com/software/license   
  9. #   
  10. ###################################################################################   
  11.   
  12. ###################################################################################   
  13. # XML_unserialize: takes raw XML as a parameter (a string)   
  14. and returns an equivalent PHP data structure   
  15. ###################################################################################   
  16. function & XML_unserialize($xml){   
  17.     $xml_parser = new XML();   
  18.     $data = $xml_parser->parse($xml);   
  19.     $xml_parser->destruct();   
  20.     return $data;   
  21. }   
  22. ###################################################################################   
  23. # XML_serialize: serializes any PHP data structure into XML   
  24. # Takes one parameter: the data to serialize. Must be an array.   
  25. ###################################################################################   
  26. function & XML_serialize($data$level = 0, $prior_key = NULL){   
  27.     if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }   
  28.     while(list($key$value) = each($data))   
  29.         if(!strpos($key' attr')) #if it's not an attribute  
  30.             #we don't treat attributes by themselves, so for an emptyempty element   
  31.             # that has attributes you still need to set the element to NULL   
  32.   
  33.             if(is_array($valueand array_key_exists(0, $value)){   
  34.                 XML_serialize($value$level$key);   
  35.             }else{   
  36.                 $tag = $prior_key ? $prior_key : $key;   
  37.                 echo str_repeat("\t"$level),'<',$tag;   
  38.                 if(array_key_exists("$key attr"$data)){ #if there's an attribute for this element  
  39.                     while(list($attr_name, $attr_value) = each($data["$key attr"]))  
  40.                         echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';  
  41.                     reset($data["$key attr"]);  
  42.                 }  
  43.  
  44.                 if(is_null($value)) echo " />\n";  
  45.                 elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";  
  46.                 else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";  
  47.             }  
  48.     reset($data);  
  49.     if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }  
  50. }  
  51. ###################################################################################  
  52. # XML class: utility class to be used with PHP's XML handling functions   
  53. ###################################################################################   
  54. class XML{   
  55.     var $parser;   #a reference to the XML parser   
  56.     var $document; #the entire XML structure built up so far   
  57.     var $parent;   #a pointer to the current parent - the parent will be an array  
  58.     var $stack;    #a stack of the most recent parent at each nesting level   
  59.     var $last_opened_tag; #keeps track of the last tag opened.   
  60.   
  61.     function XML(){   
  62.         $this->parser = &xml_parser_create();   
  63.         xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);   
  64.         xml_set_object($this->parser, $this);   
  65.         xml_set_element_handler($this->parser, 'open','close');   
  66.         xml_set_character_data_handler($this->parser, 'data');   
  67.     }   
  68.     function destruct(){ xml_parser_free($this->parser); }   
  69.     function & parse($data){   
  70.         $this->document = array();   
  71.         $this->stack    = array();   
  72.         $this->parent   = $this->document;   
  73.         return xml_parse($this->parser, $data, true) ? $this->document : NULL;   
  74.     }   
  75.     function open($parser$tag$attributes){   
  76.         $this->data = ''; #stores temporary cdata   
  77.         $this->last_opened_tag = $tag;   
  78.         if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before  
  79.             if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric  
  80.                 #this is the third or later instance of $tag we've come across   
  81.                 $key = count_numeric_items($this->parent[$tag]);   
  82.             }else{   
  83.                 #this is the second instance of $tag that we've seen. shift around  
  84.                 if(array_key_exists("$tag attr",$this->parent)){  
  85.                     $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);  
  86.                     unset($this->parent["$tag attr"]);  
  87.                 }else{  
  88.                     $arr = array(&$this->parent[$tag]);  
  89.                 }  
  90.                 $this->parent[$tag] = &$arr;  
  91.                 $key = 1;  
  92.             }  
  93.             $this->parent = &$this->parent[$tag];  
  94.         }else{  
  95.             $key = $tag;  
  96.         }  
  97.         if($attributes) $this->parent["$key attr"] = $attributes;  
  98.         $this->parent  = &$this->parent[$key];  
  99.         $this->stack[] = &$this->parent;  
  100.     }  
  101.     function data(&$parser, $data){  
  102.         if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags   
  103.             $this->data .= $data;   
  104.     }   
  105.     function close($parser$tag){   
  106.         if($this->last_opened_tag == $tag){   
  107.             $this->parent = $this->data;   
  108.             $this->last_opened_tag = NULL;   
  109.         }   
  110.         array_pop($this->stack);   
  111.         if($this->stack) $this->parent = $this->stack[count($this->stack)-1];   
  112.     }   
  113. }   
  114. function count_numeric_items($array){   
  115.     return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;   
  116. }   
  117. ?>  

附件: xml.rar (1.73 K, 下载次数:85)

Tags: php, xml

PHP5中简洁实用的SQLITE操作类

PHP5中有2种连接sqlite的方法。一种是默认提供的,另一种是PDO类。默认的只支持sqlite2,但是PDO可以间接支持sqlite3。面是泡菜写的简单的PDO类可以兼容2个版本,个人感觉很实用,所以转载过来了!

PHP代码
  1. class SQLite{   
  2.     function __construct($file){   
  3.         try{   
  4.             $this->Connection=new PDO('sqlite2:'.$file);   
  5.         }catch(PDOException $e){   
  6.             try{   
  7.                 $this->Connection=new PDO('sqlite:'.$file);   
  8.             }catch(PDOException $e){   
  9.                 exit('error!');   
  10.             }   
  11.         }   
  12.     }   
  13.     function __destruct(){   
  14.         $this->Connection=null;   
  15.     }   
  16.     function Query($SQL){   
  17.         return $this->Connection->Query($SQL);   
  18.     }   
  19.     function Execute($SQL){   
  20.         return $this->Query($SQL)->fetch();   
  21.     }   
  22.     function RecordArray($SQL){   
  23.         return $this->Query($SQL)->fetchAll();   
  24.     }   
  25.     function RecordCount($SQL){   
  26.         return count($this->RecordArray($SQL));   
  27.     }   
  28.     function RecordLastID(){   
  29.         return $this->Connection->lastInsertId();   
  30.     }   
  31. }  

然后实例化,在实例化中如果数据库存在就自动打开,不存在就会自动创建数据库。

PHP代码
  1. $DB=new SQLite('blog.db');  //这个数据库文件名字任意   

创建数据库表。

PHP代码
  1. $DB->Query("create table test(id integer primary key,title varchar(50)");   

接下来添加数据。

PHP代码
  1. $DB->Query("insert into test(title) values('泡菜')");      
  2. $DB->Query("insert into test(title) values('蓝雨')");      
  3. $DB->Query("insert into test(title) values('Ajan')");      
  4. $DB->Query("insert into test(title) values('傲雪蓝天')");    

之后就是如何读取数据了,也就是循环。

PHP代码
  1. $SQL='select title from test order by id desc';      
  2. foreach($DB->Query($SQLas $RS){      
  3.     echo $RS['title'];      
  4. }     

Tags: php, sqlite

Windows下在命令行里的提权命令

net user --------------------------查看有哪些用户

net user guest /active:yes  ------ 激活guest用户

net user 用户名 密码 /add  ------建立用户

net localgroup administrators 用户名 /add -------   把“用户”添加到管理员

Tags: windows

PHP无敌之运行CMD命令功能

前天还讲Google Apps 的自定义域名服务器已经解封,今天就发现已经失效了,万恶的GFW。今天搜索了一下没有封的IP,但是IP列表太多,其实部分已经封掉了,所以得一个一个的PING,结果在PHP中找到了这条命令,可以使用PHP自动PING IP,校检网络连接是否正常!下面就是简单的一个例子,可能只能在WINDOWS服务器下面使用了。最后想想,还可以使用PHP执行其它EXE命令呢,所以如果要在服务器上用的话,特别注意安全,嘿嘿!

PHP代码
  1. <?php   
  2. $server = 'ping kalvin.cn -n 1';   
  3. $last_line = exec($server$arr);   
  4. echo "$last_line"//最后总结结果   
  5. print_r($arr); //PING命令详细数据数组   
  6. ?>  

国外一位大师使用Sockets Ping,似乎效率更高:

PHP代码
  1. <?php   
  2.     // Checksum calculation function   
  3.     function icmpChecksum($data)   
  4.     {   
  5.     if (strlen($data)%2)   
  6.     $data .= "\x00";   
  7.        
  8.     $bit = unpack('n*'$data);   
  9.     $sum = array_sum($bit);   
  10.        
  11.     while ($sum >> 16)   
  12.     $sum = ($sum >> 16) + ($sum & 0xffff);   
  13.        
  14.     return pack('n*', ~$sum);   
  15.     }   
  16.     // Making the package   
  17.     $type"\x08";   
  18.     $code"\x00";   
  19.     $checksum"\x00\x00";   
  20.     $identifier = "\x00\x00";   
  21.     $seqNumber = "\x00\x00";   
  22.     $data"Scarface";   
  23.     $package = $type.$code.$checksum.$identifier.$seqNumber.$data;   
  24.     $checksum = icmpChecksum($package); // Calculate the checksum   
  25.     $package = $type.$code.$checksum.$identifier.$seqNumber.$data;   
  26.     // And off to the sockets   
  27.     $socket = socket_create(AF_INET, SOCK_RAW, 1);   
  28.     socket_connect($socket"www.google.com", null);   
  29.     // If you're using below PHP 5, see the manual for the microtime_float   
  30.     // function. Instead of just using the m   
  31.     //     icrotime() function.   
  32.     $startTime = microtime(true);   
  33.     socket_send($socket$packagestrLen($package), 0);   
  34.     if (socket_read($socket, 255)) {   
  35.     echo round(microtime(true) - $startTime, 4) .' seconds';   
  36.     }   
  37.     socket_close($socket);   
  38. ?>  

Tags: php, windows, google

Records:15123