php调用webservice接口程序,返回xml格式,php解析xml形成数组

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hai7425/article/details/87913198
<?php
set_time_limit(0);
ini_set('soap.wsdl_cache_enabled','0');//关闭缓存
header('Content-Type: text/html; charset=UTF-8');
$soap=new SoapClient('http://192.168.1.33:6321/services/OAInterface?wsdl',array('encoding'=>'utf-8','cache_wsdl' => 0,'compression'=>true)); 
$soap->soap_defencoding = 'utf-8'; 
$soap->xml_encoding = 'utf-8'; 
echo "<pre>";
print_r($soap->__getFunctions());
echo "</pre>";
echo "<pre>";
print_r($soap->__getTypes () ); 
echo "</pre>";
/*
 echo '__getFunctions = '; 
    var_dump($soap->__getFunctions()); 
    echo '<br/>-----------------------<br/>'; 
    echo '__getLastRequest = '.$soap->__getLastRequest(); 
    echo '<br/>-----------------------<br/>'; 
    echo '__getLastResponse = '.$soap->__getLastResponse(); 
    echo '<br/>-----------------------<br/>'; 

*/
//echo "123123";
//exit;
/*
 $result=$soap->__call('IP_DEPT_CHARGE',array("2019-02-19"));
 print_r($result);
$arr=array('2019-02-19');
$result=$soap->__Call('IP_DEPT_CHARGE',$arr);
 print_r($result);
$a="2019-02-19";
//$aa=(string)$a;
$result=$soap->IP_DEPT_CHARGE("2019-02-19");
print_r($result);
*/
$param = array('input'=>"2019-02-19");//参数拼接xml字符串 
$result = $soap->IP_DEPT_CHARGE($param);//返回值
//print_r($result);

 $arr1=object_array($result);
 //print_r($arr1);
 $re=$arr1["payload"];
 //echo $re;
 
 
 $simple1=str_replace("<![CDATA[","",$re);
$simple2=str_replace("]]>","",$simple1); //< ? xml version='1.0' encoding='utf-8' ? >
$simple3=str_replace("<?xml version='1.0' encoding='utf-8'?>","",$simple2);
$simple4=str_replace("></from>","</from>",$simple3);
$simple=str_replace("></to>","</to>",$simple4);

$simple=$re;

$postObj = simplexml_load_string($re, 'SimpleXMLElement', LIBXML_NOCDATA);

$jsonStr = json_encode($postObj);
$jsonArray = json_decode($jsonStr,true);
//echo "<pre>";
//print_r($jsonArray);
//echo "</pre>";
//echo "<hr>";
$string = <<<XML
$simple
XML;
echo $string."<br>";
$xml = simplexml_load_string($string);
$xml =  simplexml_load_string($re, 'SimpleXMLElement', LIBXML_NOCDATA);//使用此方法
print_r($xml);
$arr=object_array($xml);
echo "<pre>";
print_r($arr);
echo "</pre>";
 
 /*
 
 
 $result=$soap->__soapCall('IP_DEPT_CHARGE',array($arr));
print_r($result);
*/
/*
echo  $soap->Add(1,2);
echo "<br>";
//echo $soap->_soapCall('Add',array(1,2));//或者这样调用也可以
//echo $soap->_soapCall('Add',array(1,2));//或者这样调用也可以
echo "<br>";
echo  iconv('UTF-8', 'GB2312',$soap->Hello());//解决中文乱码的问题   从utf8转换成gb2312
echo "<br>";
echo  $soap->abc('asd');
*/

/* 
 * 对象转换成数组 
*/   
function object2Array($objParam) {   
    $obj_param = ( array )$objParam;   
    foreach ($obj_param as $key => $value) {   
        if (is_object($value)) {   
            object2Array($value);   
            $obj_param [$key] = ( array )$value;   
        }   
    }   
}

function object_array($array){ 
  if(is_object($array)){ 
    $array = (array)$array; 
  } 
  if(is_array($array)){ 
    foreach($array as $key=>$value){ 
      $array[$key] = object_array($value); 
    } 
  } 
  return $array; 
} 
?>

猜你喜欢

转载自blog.csdn.net/hai7425/article/details/87913198