存档

‘Default’ 分类的存档

通用php数组(array)与xml相互转换函数

2010年3月10日

最近做一个项目,需要和其他的程序进行对接,xml是交互数据的首选,所以,我需要一个函数能把php的数组转换成xml字符串,又同时可以把字符串再还原回php数组,其实是很简单的递归调用,发出来备用

函数定义:

/* 将数组格式化为XML字符串 */
function array2xml($array,$level = 0){
    $return = '';
    if($level == 0){
        $return = '<?xml version="1.0" encoding="utf-8" ?><root>';
    }
    foreach($array as $key => $item){
        if(!is_array($item)){
            $return .= "<item key='{$key}'>{$item}</item>";
        }else{
            $return .= "<item key='{$key}'>";
            $return .= array2xml($item,$level + 1);
            $return .= "</item>";
        }
    }
    if($level == 0){
        $return .= '</root>';
    }
    return $return;
}

/* 辅助函数用来获取DOM跟节点 */
function getXmlRoot($xml){
	$doc = new DOMDocument();
	$doc->loadXML($xml);
	$root = $doc->documentElement;
	return $root;
}

/* 将被array2xml格式化的XML还原 */
function xml2array($xml){
	$return_array = array();
	foreach($xml->childNodes as $node){
		$length = $node->childNodes->length;
		$key = $node->getAttribute('key');
		if($length == 1 and $node->firstChild->nodeType == XML_TEXT_NODE){
			$return_array[$key] = $node->nodeValue;
		}else{
			$return_array[$key] = xml2array($node);
		}
	}
	return $return_array;
}

使用方法:

阅读全文…

Jack Default, PHP , , ,

jQuery插件开发模式

2010年3月4日

原文:

http://www.learningjquery.com/2007/10/a-plugin-development-pattern

翻译:

我已经开发基于JQuery的插件有一段时间了,对于各种形式和要求的插件开发有了较好的掌握。在这里,我将在本文中分享我认为十分有用的插件开发方式。当前前提是假定你对JQuery的插件开发有一定了解,如果你是插件开发的新手,不妨先看看jQuery官网上的the jQuery Authoring Guidelines。

我认为以下插件开发模式是必须应该掌握的:

1.在JQuery命名空间内声明一个特定的命名;
2.接收参数来控制插件的行为;
3.提供公有方法访问插件的配置项值;
4.提供公有方法来访问插件中其他的方法(如果可能的话);
5.保证私有方法是私有的;
6.支持元数据插件;

下面,我将逐一讲述上面的内容,并在同时给出相关的简单插件开发代码。

阅读全文…

Jack Default, JavaScript, jQuery ,

jQuery 插件代码编写模板 jQuery-Plugin-Template

2010年3月4日

原文:

http://www.cnitblog.com/lxasp/archive/2009/08/22/60923.html

jQuery简实插件库:

http://www.lxasp.com/jq/

jQuery-Plugin-Template 代码:

阅读全文…

Jack Default, JavaScript, jQuery

Ubuntu 下使用 Doxygen

2010年2月22日

一、安装Doxygen:(官方网站:http://www.stack.nl/~dimitri/doxygen/)

sudo apt-get install doxygen doxygen-doc doxygen-gui graphviz

注:如果要生成png格式的图片,必须安装Graphviz软件。

出现下提示信息的话,则就不要安doxygen-gui了:sudo apt-get install doxygen doxygen-doc graphviz

现在没有可用的软件包 doxygen-gui,但是它被其它的软件包引用了。

E: 软件包 doxygen-gui 还没有可供安装的候选者

二、Doxygen 的工作过程可分为三个步骤:

1. 配置 Doxygen 工作环境,生成 Doxygen 配置文件;

2. 在程序源码中添加符合 Doxygen 可解析的注释格式;

3. 使用 Doxygen 解析源码,输出格式化文档。

三、Doxygen常用标签命令关键字:

1. 文件信息:

  1) @file –> 文件声明,即当前文件名

  2) @author –> 作者

  3) @version –> 版本,推荐使用$Id$

  4) @todo –> 改进,可以指定针对的版本

2. 模块信息:

  1) @var –> 模块变量说明

  2) @typedef –> 模块变量类型说明

3. 函数信息:

  1) @param –> 参数说明

  2) @arg –> 列表说明参数信息

  3) @return –> 返回值说明

  4) @retval –> 返回值类型说明

  5) @note –> 注解

4. 提醒信息:

  1) @brief –> 摘要,即当前文件说明

  2) @see –> 参看

  3) @attention –> 注意

  4) @bug –> 问题

  5) @warning –> 警告

  6) @sa –> 参考资料

Jack Default, Linux, PHP

ATI HD4500 显卡官方驱动安装

2010年2月9日

官方驱动

http://support.amd.com/us/gpudownload/Pages/index.aspx

本地下载

http://thinkly.cn/download/linux/ati-driver-installer-10-1-x86.x86_64.run

下载后执行

sudo sh ./ati-driver-installer-10-1-x86.x86_64.run
/usr/bin/aticonfig –initial

 

重启后OK

Jack Default, Linux, ati , , ,

ThinkPad 11b/g/n Wireless LAN Mini-PCI Express Adapter II

2010年2月8日

原文地址

http://www.thinkwiki.org/wiki/ThinkPad_11b/g/n_Wireless_LAN_Mini-PCI_Express_Adapter_II

驱动下载

http://launchpadlibrarian.net/33927923/rtl8192se_linux_2.6.0010.1012.2009.tar.gz

解压缩之后执行

sudo apt-get install build-essential linux-source-2.6.31

然后进入到驱动目录

sudo make
sudo su
make install

如果没有出现任何问题,最后一步

modprobe r8192se_pci

 

完事!

Jack Default, Linux , , , ,

通过SSH挂载远程目录

2010年2月5日

1. sudo apt-get install sshfs

2. 新建文件夹用于挂载远程目录:mkdir ~/home-server

3. sshfs yourname@remote-host:/path/to/your/dir ~/home-server

4. 卸载时使用 umount ~/home-server

Jack Default, Linux , ,

ubuntu 9.10 ATI HD系列显卡安装开源驱动开启3D

2010年1月29日

cURL常用的几个PHP函数

2009年12月29日
1、开启PHP的cURL功能

在Windows平台下,或者使用xampp之类的集成服务器的程序,会非常简单,你需要改一改你的php.ini文件的设置,找到php_curl.dll,并取消前面的分号注释就行了。如下所示:

//取消注释,开启cURL功能
extension=php_curl.dll

Linux下面,那么,你需要重新编译你的PHP了,编辑时,你需要打开编译参数——在configure命令上加上“–with-curl” 参数。

2、使用cURL来GET数据

cURL最简单最常用的采用GET来获取网页内容的PHP函数
function getCURL($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 3);//超时时间
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}

3、使用cURL来POST数据

当我们需要对cURL请求的页面采用POST的请求方式时,我们使用下面的PHP函数
function _curl_post($url, $vars) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
$data = curl_exec($ch);
curl_close($ch);
if ($data)
return $data;
else
return false;
}

4、使用cURL,需要HTTP服务器认证

当我们请求地址需要加上身份验证,即HTTP服务器认证的时候,我们就要使用下面的函数了,对于cURL中GET方法使用验证也是采用相同的方式。
function postCurlHTTP($url, $str) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, “验证的用户名:密码”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
$data = curl_exec($ch);
$Headers = curl_getinfo($ch);
if ($Headers['http_code'] == 200) {
return $data;
} else {
return false;
}
}

5、使用cURL获取302重定向的页面

下面函数$data为重定向后页面的内容,这里我们写一个简单的cURL POST302重定向后返回重定向页面URL的函数,有时候返回页面的URL更加重要。
function _curl_post_302($url, $vars) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
$data = curl_exec($ch);
$Headers = curl_getinfo($ch);
curl_close($ch);
if ($data&&$Headers)
return s$Headers["url"];
else
return false;
}

6、给cURL加个代理服务器

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘http://www.2fool.cn‘);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, ‘代理服务器地址(www.2fool.cn):端口’);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, ‘代理用户:密码’);
$data = curl_exec();
curl_close($ch);

7、一个cURL简单的类

<?php
/*
Sean Huber CURL library

This library is a basic implementation of CURL capabilities.
It works in most modern versions of IE and FF.

==================================== USAGE ====================================
It exports the CURL object globally, so set a callback with setCallback($func).
(Use setCallback(array(’class_name’, ‘func_name’)) to set a callback as a func
that lies within a different class)
Then use one of the CURL request methods:

get($url);
post($url, $vars); vars is a urlencoded string in query string format.

Your callback function will then be called with 1 argument, the response text.
If a callback is not defined, your request will return the response text.
*/

class CURL {
var $callback = false;

function setCallback($func_name) {
$this->callback = $func_name;
}

function doRequest($method, $url, $vars) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, ‘cookie.txt’);
curl_setopt($ch, CURLOPT_COOKIEFILE, ‘cookie.txt’);
if ($method == ‘POST’) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
if ($this->callback)
{
$callback = $this->callback;
$this->callback = false;
return call_user_func($callback, $data);
} else {
return $data;
}
} else {
return curl_error($ch);
}
}

function get($url) {
return $this->doRequest(‘GET’, $url, ‘NULL’);
}

function post($url, $vars) {
return $this->doRequest(‘POST’, $url, $vars);
}
}
?>

Jack Default, PHP ,

php 模拟POST提交的2种方法

2009年12月29日

1.通过curl函数

$post_data = array();
$post_data['clientname'] = "test08";
$post_data['clientpasswd'] = "test08";
$post_data['submit'] = "submit";
$url=’http://xxx.xxx.xxx.xx/xx/xxx/top.php’;
$o="";
foreach ($post_data as $k=>$v)
{
$o.= "$k=".urlencode($v)."&";
}
$post_data=substr($o,0,-1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
//为了支持cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, ‘cookie.txt’);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);

2.通过fsockopen

$URL=‘http://xxx.xxx.xxx.xx/xx/xxx/top.php’;
$post_data['clientname'] = "test08";
$post_data['clientpasswd'] = "test08";
$post_data['submit'] = "ログイン";
$referrer="";
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer=$_SERVER["SCRIPT_URI"];
// making string from $data
foreach($post_data as $key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);
// Find out which port is needed – if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referrer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n";
$request.="\n";
$request.=$data_string."\n";
$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);

Jack Default, PHP ,