php xml-rpc远程调用
|
<div class="codetitle"><a style="CURSOR: pointer" data="30576" class="copybut" id="copybut30576" onclick="doCopy('code30576')"> 代码如下:<div class="codebody" id="code30576"> Received the following raw request: " . XMLRPC_show($request,'print_r',true));} $data = &XML_unserialize($request); if(defined('XMLRPC_DEBUG') and XMLRPC_DEBUG){ XMLRPC_debug('XMLRPC_parse'," Returning the following parsed request: " . XMLRPC_show($data,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'){ #it's 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{ #it's 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 it's 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{ #it's 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)){ #it's 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 n"; $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[$n] = &XMLRPC_adjustValue($temp[$n]); } $temp = $temp2; }else{ $temp2 = &XMLRPC_adjustValue($temp); $temp = array($temp2); #I do the temp assignment because it avoids copying, # since I can put a reference in the array #PHP's reference model is a bit silly,and I can't just say: # $temp = array(&XMLRPC_adjustValue($temp)); } } }elseif(isset($current_node['struct'])){ if(!is_array($current_node['struct'])){ #If there are no members,return an emptyempty array return array(); }else{ #echo "Getting rid of struct -> member n"; $temp = $current_node['struct']['member']; if(is_array($temp) and array_key_exists(0,$temp)){ $count = count($temp); for($n=0;$n<$count;$n++){ #echo "Passing name {$temp[$n][name]}. Value is: " . show($temp[$n][value],var_dump,true) . " n"; $temp2[$temp[$n]['name']] = &XMLRPC_adjustValue($temp[$n]['value']); #echo "adjustValue(): After assigning,the value is " . show($temp2[$temp[$n]['name']],true) . " n"; } }else{ #echo "Passing name $temp[name] n"; $temp2[$temp['name']] = &XMLRPC_adjustValue($temp['value']); } $temp = $temp2; } }else{ $types = array('string','int','i4','double','dateTime.iso8601','base64','boolean'); $fell_through = true; foreach($types as $type){ if(array_key_exists($type,$current_node)){ #echo "Getting rid of '$type' n"; $temp = $current_node[$type]; #echo "adjustValue(): The current node is set with a type of $type n"; $fell_through = false; break; } } if($fell_through){ $type = 'string'; #echo "Fell through! Type is $type n"; } switch ($type){ case 'int': case 'i4': $temp = (int)$temp; break; case 'string': $temp = (string)$temp; break; case 'double': $temp = (double)$temp; break; case 'boolean': $temp = (bool)$temp; break; } } }else{ $temp = (string)$current_node; } return $temp; } function XMLRPC_getParams($request){ if(!is_array($request['methodCall']['params'])){ #If there are no parameters,return an emptyempty array return array(); }else{ #echo "Getting rid of methodCall -> params -> param n"; $temp = $request['methodCall']['params']['param']; if(is_array($temp) and array_key_exists(0,$temp)){ $count = count($temp); for($n = 0; $n < $count; $n++){ #echo "Serializing parameter $n "; $temp2[$n] = &XMLRPC_adjustValue($temp[$n]['value']); } }else{ $temp2[0] = &XMLRPC_adjustValue($temp['value']); } $temp = $temp2; return $temp; } } function XMLRPC_getMethodName($methodCall){ #returns the method name return $methodCall['methodCall']['methodName']; } function XMLRPC_request($site,$location,$methodName,$params = NULL,$user_agent = NULL){ $site = explode(':',$site); if(isset($site[1]) and is_numeric($site[1])){ $port = $site[1]; }else{ $port = 80; } $site = $site[0]; $data["methodCall"]["methodName"] = $methodName; $param_count = count($params); if(!$param_count){ $data["methodCall"]["params"] = NULL; }else{ for($n = 0; $n<$param_count; $n++){ $data["methodCall"]["params"]["param"][$n]["value"] = $params[$n]; } } $data = XML_serialize($data); if(defined('XMLRPC_DEBUG') and XMLRPC_DEBUG){ XMLRPC_debug('XMLRPC_request'," (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
