方式1:
//字段的詳細說明 請參考方式2的請求數據$xmlData
$wsdl='wsdl的遠程地址或者本地文件地址'
$soap = new SoapClient($wsdl,array("trace" => 1));
//請求頭里的字段值
$param['userName']='*****';
$param['password']='*****';
$header = new SoapHeader('命名空間', '請求頭名稱,參考對應的wsdl文件', $param, false, SOAP_ACTOR_NEXT); //生成請求頭
//發(fā)送請求的參數,請參考wsdl文件,一定要注意格式
$methodParams = array(
'userInfo'=>array('username'=>'test')
);
// UserInfo 代表請求的wsdl節(jié)點里面的某一個方法
$result = $soap->__soapCall('UserInfo',$methodParams,null,$header);
if(is_soap_fault($result)){
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}else{
//這里返回的是類,必須使用->得到元素的值
$result = $result->UserInfoResult;//根據請求的方法名加上Result即可
//具體字段可能需要轉Xml為數組,請自行百度xml轉數組方法
$result = $this->xmlToArray($result);
}
方式2:
// Header MySoapHeader 代表請求頭名稱 xmlns的值代表命名空間 請求頭里面的userName password 代表要頭部信息的字段,請根據自己的替換對應的字段值
// Body UserInfo就是要請求的方法名字 xmlns同樣代表命名空間,username 代表發(fā)送請求的字段 方式一的數據拼接可以參考以下數據
$xmldata = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<MySoapHeader xmlns="http://tempuri.org/">
<userName>*******</userName>
<password>*******</password>
</MySoapHeader>
</soap12:Header>
<soap12:Body>
<UserInfo xmlns="http://tempuri.org/">
<username>test</username>
</UserInfo>
</soap12:Body>
</soap12:Envelope>
EOT;
$wsdl='wsdl地址';
//注意 __doRequest 里面的2 代表soap的版本 我這里是2
try{
$params = array('track' => 1);
$client = new SoapClient($wsdl,$params);
$result = $client->__doRequest($xmldata,$wsdl,'UserInfo',2,0);
}catch (SoapFault $e){
echo $e->getMessage();
}catch(Exception $e){
echo $e->getMessage();
}
總結:主要是一定要注意好版本,對應wsdl文件的數據格式,錯了一點點都是不行的,包括數據的請求格式