PHP头条
热点:

PHP xml-rpc远程调用


从网上找来的XML-RPC库,对于开发小型的外部通讯接口很有用,把这个代码保存为xml-rpc.inc.php

<?php
/*
从网上找来的XML-RPC库,对于开发小型的外部通讯接口很有用
*/
function & XML_serialize($data, $level = 0, $prior_key = NULL){
#assumes a hash, keys are the variable names
$xml_serialized_string = "";
while(list($key, $value) = each($data)){
$inline = false;
$numeric_array = false;
$attributes = "";
#echo "My current key is $key, called with prior key $prior_key<br>";
if(!strstr($key, " attr")){ #if its not an attribute
if(array_key_exists("$key attr", $data)){
while(list($attr_name, $attr_value) = each($data["$key attr"])){
#echo "Found attribute $attribute_name with value $attribute_value<br>";
$attr_value = &htmlspecialchars($attr_value, ENT_QUOTES);
$attributes .= " $attr_name="$attr_value"";
}
}
if(is_numeric($key)){
#echo "My current key ($key) is numeric. My parent key is $prior_key<br>";
$key = $prior_key;
}else{
#you cant have numeric keys at two levels in a row, so this is ok
#echo "Checking to see if a numeric key exists in data.";
if(is_array($value) and array_key_exists(0, $value)){
# echo " It does! Calling myself as a result of a numeric array.<br>";
$numeric_array = true;
$xml_serialized_string .= XML_serialize($value, $level, $key);
}
#echo "<br>";
}
if(!$numeric_array){
$xml_serialized_string .= str_repeat(" ", $level) . "<$key$attributes>";
if(is_array($value)){
$xml_serialized_string .= " " . XML_serialize($value, $level+1);
}else{
$inline = true;
$xml_serialized_string .= htmlspecialchars($value);
}
$xml_serialized_string .= (!$inline ? str_repeat(" ", $level) : "") . "</$key> ";
}
}else{
#echo "Skipping attribute record for key $key<bR>";
}
}
if($level == 0){
$xml_serialized_string = "<?xml version="1.0" ?> " . $xml_serialized_string;
return $xml_serialized_string;
}else{
return $xml_serialized_string;
}
}
class XML {
var $parser; #a reference to the XML parser
var $document; #the entire XML structure built up so far
var $current; #a pointer to the current item - what is this
var $parent; #a pointer to the current parent - the parent will be an array
var $parents; #an array of the most recent parent at each level
var $last_opened_tag;
function XML($data=null){
$this->parser = xml_parser_create();
xml_parser_set_option ($this->parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, "open", "close");
xml_set_character_data_handler($this->parser, "data");
# register_shutdown_function(array($this, destruct));
}
function destruct(){
xml_parser_free($this->parser);
}
function parse($data){
$this->document = array();
$this->parent = $this->document;
$this->parents = array();
$this->last_opened_tag = NULL;
xml_parse($this->parser, $data);
return $this->document;
}
function open($parser, $tag, $attributes){
#echo "Opening tag $tag<br> ";
$this->data = "";
$this->last_opened_tag = $tag; #tag is a string
if(array_key_exists($tag, $this->parent)){
#echo "Theres already an instance of $tag at the current level ($level)<br> ";
if(is_array($this->parent[$tag]) and array_key_exists(0, $this->parent[$tag])){ #if the keys are numeric
#need to make sure theyre numeric (account for attributes)
$key = count_numeric_items($this->parent[$tag]);
#echo "There are $key instances: the keys are numeric.<br> ";
}else{
#echo "There is only one instance. Shifting everything around<br> ";
$temp = $this->parent[$tag];
unset($this->parent[$tag]);
$this->parent[$tag][0] = $temp;
if(array_key_exists("$tag attr", $this->parent)){
#shift the attributes around too if they exist
$temp = $this->parent["$tag attr"];
unset($this->parent["$tag attr"]);
$this->parent[$tag]["0 attr"] = $temp;
}
$key = 1;
}
$this->parent = $this->parent[$tag];
}else{
$key = $tag;
}
if($attributes){
$this->parent["$key attr"] = $attributes;
}
$this->parent[$key] = array();
$this->parent = $this->parent[$key];
array_unshift($this->parents, $this->parent);
}
function data($parser, $data){
#echo "Data is ", htmlspecialchars($data), "<br> ";
if($this->last_opened_tag != NULL){
$this->data .= $data;
}
}
function close($parser, $tag){
#echo "Close tag $tag<br> ";
if($this->last_opened_tag == $tag){
$this->parent = $this->data;
$this->last_opened_tag = NULL;
}
array_shift($this->parents);
$this->parent = $this->parents[0];
}
}
function & XML_unserialize($xml){
$xml_parser = new XML();
$data = $xml_parser->parse($xml);
$xml_parser->destruct();
return $data;
}
function & XMLRPC_parse($request){
if(defined(XMLRPC_DEBUG) and XMLRPC_DEBUG){
XMLRPC_debug(XMLRPC_parse, "<p>Received the following raw request:</p>" . XMLRPC_show($request, print_r, true));
}
$data = &XML_unserialize($request);
if(defined(XMLRPC_DEBUG) and XMLRPC_DEBUG){
XMLRPC_debug(XMLRPC_parse, "<p>Returning the following parsed request:</p>" . XMLRPC_show($data, print_r, true));
}
return $data;
}
function & XMLRPC_prepare($data, $type = NULL){
if(is_array($data)){
$num_elements = count($data);
if((array_key_exists(0, $data) or !$num_elements) and $type != struct){ #its an array
if(!$num_elements){ #if the array is emptyempty
$returnvalue = array(array => array(data => NULL));
}else{
$returnvalue[array][data][value] = array();
$temp = $returnvalue[array][data][value];
$count = count_numeric_items($data);
for($n=0; $n<$count; $n++){
$type = NULL;
if(array_key_exists("$n type", $data)){
$type = $data["$n type"];
}
$temp[$n] = XMLRPC_prepare($data[$n], $type);
}
}
}else{ #its a struct
if(!$num_elements){ #if the struct is emptyempty
$returnvalue = array(struct => NULL);
}else{
$returnvalue[struct][member] = array();
$temp = $returnvalue[struct][member];
while(list($key, $value) = each($data)){
if(substr($key, -5) != type){ #if its not a type specifier
$type = NULL;
if(array_key_exists("$key type", $data)){
$type = $data["$key type"];
}
$temp[] = array(name => $key, value => XMLRPC_prepare($value, $type));
}
}
}
}
}else{ #its a scalar
if(!$type){
if(is_int($data)){
$returnvalue[int] = $data;
return $returnvalue;
}elseif(is_float($data)){
$returnvalue[double] = $data;
return $returnvalue;
}elseif(is_bool($data)){
$returnvalue[boolean] = ($data ? 1 : 0);
return $returnvalue;
}elseif(preg_match(/^d{8}Td{2}:d{2}:d{2}$/, $data, $matches)){ #its a date
$returnvalue[dateTime.iso8601] = $data;
return $returnvalue;
}elseif(is_string($data)){
$returnvalue[string] = htmlspecialchars($data);
return $returnvalue;
}
}else{
$returnvalue[$type] = htmlspecialchars($data);
}
}
return $returnvalue;
}
function & XMLRPC_adjustValue($current_node){
if(is_array($current_node)){
if(isset($current_node[array])){
if(!is_array($current_node[array][data])){
#If there are no elements, return an emptyempty array
return array();
}else{
#echo "Getting rid of array -> data -> value<br> ";
$temp = $current_node[array][data][value];
if(is_array($temp) and array_key_exists(0, $temp)){
$count = count($temp);
for($n=0;$n<$count;$n++){
$temp2

www.phpzy.comtrue/phprm/28595.htmlTechArticlePHP xml-rpc远程调用 从网上找来的XML-RPC库,对于开发小型的外部通讯接口很有用,把这个代码保存为xml-rpc.inc.php ?php /* 从网上找来的XML-RPC库,对于开发小型的外部通讯接口很有用 */ fun...

相关文章

    暂无相关文章

PHP之友评论

今天推荐