php解析JSON与XML的小例子,供大家学习参考。
1、解析JSON数据
$json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} ';
$obj=json_decode()($json_string);
echo $obj->name; //prints foo
echo $obj->interest[1]; //prints php
?>
2、解析XML 数据
I)、xml文件
<users>
<user id='398'>
<name>Foo</name>
<email>foo@bar.com</name>
</user>
<user id='867'>
<name>Foobar</name>
<email>foobar@foo.com</name>
</user>
</users>";
II)、解析xml的代码
<?php
//load the xml string using simplexml
$xml = simplexml_load_string($xml_string);
//loop through the each node of user
foreach ($xml->user as $user)
{
//access attribute
echo $user['id'], ' ';
//subnodes are accessed by -> operator
echo $user->name, ' ';
echo $user->email, '<br />';
}
?>
在对方服务器使用了反向代理的情况下,用$_SERVER['REMOTE_ADDR']获取用户IP,那么HTTP头中REMOTE_ADDR就不是用户的地址,而是上一级代理的IP地址。
网上提供的解决方法很多, 之前也发过一些相关文章,大家可以看看:
php 获取客户端IP地址、地理信息、浏览器信息、本地真实IP的代码
php获取准确的客户端IP地址的代码
php获取客户端IP地址的四种方法
PHP获取真实的客户IP的方法
这里再为大家提供二种获取客户端真实IP的方法。
方法1:使用curl
function get_onlineip() {
$ch = curl_init('http://www.ip138.com/ip2city.asp');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch);
preg_match('/\[(.*)\]/', $a, $ip);
return $ip[1];
}
?>
方法2:使用$_SERVER['HTTP_X_FORWARDED_FOR']
function get_onlineip() {
$onlineip = '';
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset()($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
return $onlineip;
}
?>
在php开发中,经常会用到XML文件作为配置文件,或作为轻量级的数据库,还是挺好用的。
只要XML文件大小小于2M,在php下的读取速度还是不错的。
这里为大家提供一个自己写的php xml操作类,仅供大家学习参考。
如果需要循环利用XML作为数据库,只要记录节点总数,当到达一个饱和数量时,再重新初始化XML即可。
1、xml操作类
<?php
/**
php xml操作类
link:http://www.
*/
class requirelog
{
protected static $requirelog;
//get ready for xml,if xml is not found ,create xml
static function _initializeLog()
{
if(file_exists('requirelog.xml'))
{
self::$requirelog = DOMDocument::load('requirelog.xml');
}
else
{
self::$requirelog = new DOMDocument('1.0', 'iso-8859-1');
self::$requirelog->loadXML('<?xml version="1.0" encoding="UTF-8"?><contents></contents>');
self::$requirelog->save('requirelog.xml');
}
}
//获取finish字段全部为0的所有节点
public static function get_entrys()
{
self::_initializeLog();
$entrys = self::$requirelog->getElementsByTagName('entry');
$entry_arrays = array();
foreach($entrys as $entry)
{
if($entry->getElementsByTagName('finish')->item(0)->nodeValue == 0)
{
$entry_array = array();
$entry_array['id'] = $entry->getElementsByTagName('id')->item(0)->nodeValue;
$entry_array['time'] = $entry->getElementsByTagName('time')->item(0)->nodeValue;
$entry_array['operator'] = $entry->getElementsByTagName('operator')->item(0)->nodeValue;
$entry_array['byoperator'] = $entry->getElementsByTagName('byoperator')->item(0)->nodeValue;
$entry_array['parameters1'] = $entry->getElementsByTagName('parameters1')->item(0)->nodeValue;
$entry_array['parameters2'] = $entry->getElementsByTagName('parameters2')->item(0)->nodeValue;
$entry_array['parameters3'] = $entry->getElementsByTagName('parameters3')->item(0)->nodeValue;
$entry_array['type'] = $entry->getElementsByTagName('type')->item(0)->nodeValue;
$entry_array['module'] = $entry->getElementsByTagName('module')->item(0)->nodeValue;
$entry_array['finish'] = $entry->getElementsByTagName('finish')->item(0)->nodeValue;
$entry_arrays[] = $entry_array;
}
}
return $entry_arrays;
}
//获取所有的节点
public static function get_count_entrys()
{
self::_initializeLog();
$entrys = self::$requirelog->getElementsByTagName('entry');
$entry_arrays = array();
foreach($entrys as $entry)
{
$entry_array = array();
$entry_array['id'] = $entry->getElementsByTagName('id')->item(0)->nodeValue;
$entry_array['time'] = $entry->getElementsByTagName('time')->item(0)->nodeValue;
$entry_array['operator'] = $entry->getElementsByTagName('operator')->item(0)->nodeValue;
$entry_array['byoperator'] = $entry->getElementsByTagName('byoperator')->item(0)->nodeValue;
$entry_array['parameters1'] = $entry->getElementsByTagName('parameters1')->item(0)->nodeValue;
$entry_array['parameters2'] = $entry->getElementsByTagName('parameters2')->item(0)->nodeValue;
$entry_array['parameters3'] = $entry->getElementsByTagName('parameters3')->item(0)->nodeValue;
$entry_array['type'] = $entry->getElementsByTagName('type')->item(0)->nodeValue;
$entry_array['module'] = $entry->getElementsByTagName('module')->item(0)->nodeValue;
$entry_array['finish'] = $entry->getElementsByTagName('finish')->item(0)->nodeValue;
$entry_arrays[] = $entry_array;
}
return $entry_arrays;
}
//根据ID获取节点
public static function get_entry($id)
{
self::_initializeLog();
$count=count(self::get_count_entrys());
if($count > 0)
{
$entrys = self::$requirelog->getElementsByTagName('entry');
foreach($entrys as $entry)
{
if ($id == $entry->getElementsByTagName('id')->item(0)->nodeValue)
{
$entry_array = array();
$entry_array['id'] = $entry->getElementsByTagName('id')->item(0)->nodeValue;
$entry_array['time'] = $entry->getElementsByTagName('time')->item(0)->nodeValue;
$entry_array['operator'] = $entry->getElementsByTagName('operator')->item(0)->nodeValue;
$entry_array['byoperator'] = $entry->getElementsByTagName('byoperator')->item(0)->nodeValue;
$entry_array['parameters1'] = $entry->getElementsByTagName('parameters1')->item(0)->nodeValue;
$entry_array['parameters2'] = $entry->getElementsByTagName('parameters2')->item(0)->nodeValue;
$entry_array['parameters3'] = $entry->getElementsByTagName('parameters3')->item(0)->nodeValue;
$entry_array['type'] = $entry->getElementsByTagName('type')->item(0)->nodeValue;
$entry_array['module'] = $entry->getElementsByTagName('module')->item(0)->nodeValue;
$entry_array['finish'] = $entry->getElementsByTagName('finish')->item(0)->nodeValue;
return $entry_array;
}
}
}
else
return null;
}
//执行完用户请求,更改finsh字段
public static function do_finsh($id)
{
self::_initializeLog();
$entrys = self::$requirelog->getElementsByTagName('entry');
$entry_arrays = array();
foreach($entrys as $entry)
{
if($entry->getElementsByTagName('id')->item(0)->nodeValue == $id)
{
$entry->getElementsByTagName('finish')->item(0)->nodeValue = 1;
}
}
self::$requirelog->save('requirelog.xml');
}
//增加新节点,$operator为操作者,$byoperator是被操作对象,$parameters1,2,3为可扩展的参数,$type为类型,$module为影响的模块
static function add_entry($operator,$byoperator,$parameters1,$parameters2,$parameters3,$type,$module)
{
//$contents = self::$restlog->getElementsByTagName('contents');
self::_initializeLog();
$count=count(self::get_count_entrys());
$entry = self::$requirelog->createElement('entry');
$id = self::$requirelog->createElement('id');
$idvalue = self::$requirelog->createTextNode($count+1);
$id->appendChild($idvalue);
$time = self::$requirelog->createElement('time');
$timevalue = self::$requirelog->createTextNode(date("Y-m-d H:i:s",time()));
$time->appendChild($timevalue);
$op = self::$requirelog->createElement('operator');
$opvalue = self::$requirelog->createTextNode($operator);
$op->appendChild($opvalue);
$bop = self::$requirelog->createElement('byoperator');
$bopvalue = self::$requirelog->createTextNode($byoperator);
$bop->appendChild($bopvalue);
$ps1 = self::$requirelog->createElement('parameters1');
$ps1value = self::$requirelog->createTextNode($parameters1);
$ps1->appendChild($ps1value);
$ps2 = self::$requirelog->createElement('parameters2');
$ps2value = self::$requirelog->createTextNode($parameters2);
$ps2->appendChild($ps2value);
$ps3 = self::$requirelog->createElement('parameters3');
$ps3value = self::$requirelog->createTextNode($parameters3);
$ps3->appendChild($ps3value);
$tp = self::$requirelog->createElement('type');
$tpvalue = self::$requirelog->createTextNode($type);
$tp->appendChild($tpvalue);
$md = self::$requirelog->createElement('module');
$mdvalue = self::$requirelog->createTextNode($module);
$md->appendChild($mdvalue);
$fi = self::$requirelog->createElement('finish');
$fivalue = self::$requirelog->createTextNode(0);
$fi->appendChild($fivalue);
$entry->appendChild($id);
$entry->appendChild($time);
$entry->appendChild($op);
$entry->appendChild($bop);
$entry->appendChild($ps1);
$entry->appendChild($ps2);
$entry->appendChild($ps3);
$entry->appendChild($tp);
$entry->appendChild($md);
$entry->appendChild($fi);
self::$requirelog->documentElement->appendChild($entry);
self::$requirelog->save('requirelog.xml');
return 1;
}
}
?>
2、requirelog.xml文件,参考格式如下:
<contents>
<entry>
<id>1</id>
<time>2009-11-21 16:56:33</time>
<operator>peng.zhou</operator>
<byoperator>zxcxzcxzczxcxz</byoperator>
<parameters1>cxzcxzc</parameters1>
<parameters2/>
<parameters3/>
<type>add_group</type>
<module>cn_group</module>
<finish>1</finish>
</entry>
<entry>
<id>2</id>
<time>2009-11-21 16:56:53</time>
<operator>peng.zhou</operator>
<byoperator>sales</byoperator>
<parameters1/>
<parameters2/>
<parameters3/>
<type>add_member</type>
<module>mail_group</module>
<finish>1</finish>
</entry>
</contents>