项目中应用到了PHP接收XML,以前没有接触过这块所以这里小记一下
上代码:
protected function getCustomerUserId( $username ){
// 载入 nusoap库
$this->load->library('Nusoap_lib');
$api_url = "http://*****/interface/ucenter/usercenterinterface.php?wsdl"; //远程服务器接口地
$this->nusoap_client = new nusoap_client( $api_url ,true );
$this->nusoap_client->soap_defencoding = 'utf-8';
$this->nusoap_client->decode_utf8 = false;
$this->nusoap_client->xml_encoding = 'utf-8';
$fieldxml = '<fields><user><field>id,username</field></user></fields>';
$wherexml = '<fields><user><username op="=">'.$username.'</username></user></fields>';
// 以上是通过PHP的NUSOAP组建调用webservice接口, 这里一并贴出来了,
// 真正的解析方法就下面几行
$parameters = array( 'field_xml' => $fieldxml,
'where_xml' => $wherexml,
'order_xml' => '<fields><user><id>asc</id></user></fields>',
'type' => 'all',
'siteflag' => 'phptest',
'pkey' => md5('phptestphptest'),
'cs' => 'utf-8'
);
// 创建DOM 文档
$xml = new DOMDocument();
$xml->loadXML( $this->nusoap_client->call(
'get_user_info',
$parameters
)) ;
// 以上是把返回的XML字符串装入DOM中,下面就可以直接索取值了。
$user_id = $xml->getElementsByTagName('id')->item(0)->nodeValue;
echo $user_id;
die;
return $result;
}
最新评论: