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

Tags: 博客
其它┊乱七八糟 | 评论:3
| 阅读:3567
Submitted by 孤魂 on 2009, March 31, 11:15 AM
昨天申请了阿里妈妈淘客广告的API,客服告诉我需要两三天的时间才可以审核下来,不知道运气如何了,呵呵,API一般都是XML传递数据来的,所以先发一个比较好用的PHP XML读取类。
今天晚上更新了本机的PHP环境配置,刚才去下载APMServ的时候,发现张宴也推荐了这个PHP读取XML的类,不过他写得更详细,还有使用方法来的,如果不知道怎么样使用的朋友可以参考参考:http://blog.s135.com/post/253/
PHP代码
- <?php
- ###################################################################################
- #
- # XML Library, by Keith Devens, version 1.2b
- # http:
- #
- # This code is Open Source, released under terms similar to the Artistic License.
- # Read the license at http:
- #
- ###################################################################################
-
- ###################################################################################
- # XML_unserialize: takes raw XML as a parameter (a string)
- # and returns an equivalent PHP data structure
- ###################################################################################
- function & XML_unserialize($xml){
- $xml_parser = new XML();
- $data = $xml_parser->parse($xml);
- $xml_parser->destruct();
- return $data;
- }
- ###################################################################################
- # XML_serialize: serializes any PHP data structure into XML
- # Takes one parameter: the data to serialize. Must be an array.
- ###################################################################################
- function & XML_serialize($data, $level = 0, $prior_key = NULL){
- if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
- while(list($key, $value) = each($data))
- if(!strpos($key, ' attr')) #if it's not an attribute
- #we don't treat attributes by themselves, so for an emptyempty element
- # that has attributes you still need to set the element to NULL
-
- if(is_array($value) and array_key_exists(0, $value)){
- XML_serialize($value, $level, $key);
- }else{
- $tag = $prior_key ? $prior_key : $key;
- echo str_repeat("\t", $level),'<',$tag;
- if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
- while(list($attr_name, $attr_value) = each($data["$key attr"]))
- echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
- reset($data["$key attr"]);
- }
-
- if(is_null($value)) echo " />\n";
- elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
- else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
- }
- reset($data);
- if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
- }
- ###################################################################################
- # XML class: utility class to be used with PHP's XML handling functions
- ###################################################################################
- class XML{
- var $parser; #a reference to the XML parser
- var $document; #the entire XML structure built up so far
- var $parent; #a pointer to the current parent - the parent will be an array
- var $stack; #a stack of the most recent parent at each nesting level
- var $last_opened_tag; #keeps track of the last tag opened.
-
- function XML(){
- $this->parser = &xml_parser_create();
- xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
- xml_set_object($this->parser, $this);
- xml_set_element_handler($this->parser, 'open','close');
- xml_set_character_data_handler($this->parser, 'data');
- }
- function destruct(){ xml_parser_free($this->parser); }
- function & parse($data){
- $this->document = array();
- $this->stack = array();
- $this->parent = $this->document;
- return xml_parse($this->parser, $data, true) ? $this->document : NULL;
- }
- function open($parser, $tag, $attributes){
- $this->data = ''; #stores temporary cdata
- $this->last_opened_tag = $tag;
- if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
- if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
- #this is the third or later instance of $tag we've come across
- $key = count_numeric_items($this->parent[$tag]);
- }else{
- #this is the second instance of $tag that we've seen. shift around
- if(array_key_exists("$tag attr",$this->parent)){
- $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
- unset($this->parent["$tag attr"]);
- }else{
- $arr = array(&$this->parent[$tag]);
- }
- $this->parent[$tag] = &$arr;
- $key = 1;
- }
- $this->parent = &$this->parent[$tag];
- }else{
- $key = $tag;
- }
- if($attributes) $this->parent["$key attr"] = $attributes;
- $this->parent = &$this->parent[$key];
- $this->stack[] = &$this->parent;
- }
- function data(&$parser, $data){
- if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
- $this->data .= $data;
- }
- function close($parser, $tag){
- if($this->last_opened_tag == $tag){
- $this->parent = $this->data;
- $this->last_opened_tag = NULL;
- }
- array_pop($this->stack);
- if($this->stack) $this->parent = $this->stack[count($this->stack)-1];
- }
- }
- function count_numeric_items($array){
- return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
- }
- ?>
附件: xml.rar (1.73 K, 下载次数:85)
Tags: php, xml
学习┊取长补短 | 评论:0
| 阅读:3151
Submitted by 孤魂 on 2009, March 29, 7:52 PM
PHP5中有2种连接sqlite的方法。一种是默认提供的,另一种是PDO类。默认的只支持sqlite2,但是PDO可以间接支持sqlite3。面是泡菜写的简单的PDO类可以兼容2个版本,个人感觉很实用,所以转载过来了!
PHP代码
- class SQLite{
- function __construct($file){
- try{
- $this->Connection=new PDO('sqlite2:'.$file);
- }catch(PDOException $e){
- try{
- $this->Connection=new PDO('sqlite:'.$file);
- }catch(PDOException $e){
- exit('error!');
- }
- }
- }
- function __destruct(){
- $this->Connection=null;
- }
- function Query($SQL){
- return $this->Connection->Query($SQL);
- }
- function Execute($SQL){
- return $this->Query($SQL)->fetch();
- }
- function RecordArray($SQL){
- return $this->Query($SQL)->fetchAll();
- }
- function RecordCount($SQL){
- return count($this->RecordArray($SQL));
- }
- function RecordLastID(){
- return $this->Connection->lastInsertId();
- }
- }
然后实例化,在实例化中如果数据库存在就自动打开,不存在就会自动创建数据库。
PHP代码
- $DB=new SQLite('blog.db');
创建数据库表。
PHP代码
- $DB->Query("create table test(id integer primary key,title varchar(50)");
接下来添加数据。
PHP代码
- $DB->Query("insert into test(title) values('泡菜')");
- $DB->Query("insert into test(title) values('蓝雨')");
- $DB->Query("insert into test(title) values('Ajan')");
- $DB->Query("insert into test(title) values('傲雪蓝天')");
之后就是如何读取数据了,也就是循环。
PHP代码
- $SQL='select title from test order by id desc';
- foreach($DB->Query($SQL) as $RS){
- echo $RS['title'];
- }
Tags: php, sqlite
学习┊取长补短 | 评论:1
| 阅读:3407
Submitted by 孤魂 on 2009, March 25, 12:52 AM
net user --------------------------查看有哪些用户
net user guest /active:yes ------ 激活guest用户
net user 用户名 密码 /add ------建立用户
net localgroup administrators 用户名 /add ------- 把“用户”添加到管理员
Tags: windows
学习┊取长补短 | 评论:0
| 阅读:3172
Submitted by 孤魂 on 2009, March 24, 11:28 PM
前天还讲Google Apps 的自定义域名服务器已经解封,今天就发现已经失效了,万恶的GFW。今天搜索了一下没有封的IP,但是IP列表太多,其实部分已经封掉了,所以得一个一个的PING,结果在PHP中找到了这条命令,可以使用PHP自动PING IP,校检网络连接是否正常!下面就是简单的一个例子,可能只能在WINDOWS服务器下面使用了。最后想想,还可以使用PHP执行其它EXE命令呢,所以如果要在服务器上用的话,特别注意安全,嘿嘿!
PHP代码
- <?php
- $server = 'ping kalvin.cn -n 1';
- $last_line = exec($server, $arr);
- echo "$last_line";
- print_r($arr);
- ?>
国外一位大师使用Sockets Ping,似乎效率更高:
PHP代码
- <?php
-
- function icmpChecksum($data)
- {
- if (strlen($data)%2)
- $data .= "\x00";
-
- $bit = unpack('n*', $data);
- $sum = array_sum($bit);
-
- while ($sum >> 16)
- $sum = ($sum >> 16) + ($sum & 0xffff);
-
- return pack('n*', ~$sum);
- }
-
- $type= "\x08";
- $code= "\x00";
- $checksum= "\x00\x00";
- $identifier = "\x00\x00";
- $seqNumber = "\x00\x00";
- $data= "Scarface";
- $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
- $checksum = icmpChecksum($package);
- $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
-
- $socket = socket_create(AF_INET, SOCK_RAW, 1);
- socket_connect($socket, "www.google.com", null);
-
-
-
- $startTime = microtime(true);
- socket_send($socket, $package, strLen($package), 0);
- if (socket_read($socket, 255)) {
- echo round(microtime(true) - $startTime, 4) .' seconds';
- }
- socket_close($socket);
- ?>
Tags: php, windows, google
学习┊取长补短 | 评论:0
| 阅读:3624