如果您使用內置的庫PHP,它解析響應並返回一個混合的對象/數組對象,它是與比XML無窮容易處理
編輯:由於您使用的是PHP內置的客戶端,因此這裏是我編寫的一個簡單的類。它 「變平」 的反響,可以很容易地檢索像responces:
$soap = new SOAP($wsdl, $options);
$soap->call("stuff goes here");
$soap->find("what you are looking for goes here");
/**
* @author Troy Knapp
* @copyright 2011
*
* @version .1.1
*/
class Soap {
//***VARIABLES***//
var $request; //..............string; holds last soap request
var $requestHeaders; //.......string; holds the headers for the last request
var $response; //.............string; xml response
var $responseHeaders; //......string; holds the headers for the last response
var $result; //...............array; the soap response parsed into an array
var $wsdlLocation; //.........string; url for the wsdl
var $parameters; //...........array; saved array of parameters
var $function; //.............string; name of function to be accessed
var $findResult = array();
var $flatArray = array(); //..array; holds an easy to search array
//
//***OBJECTS***//
var $client; //...................instance of SoapClient
var $exception; //................obj; SoapFault exception object
//
//***DEFAULTS***//
public $options = array(
'trace' => 1
);
function __construct($wsdl, $options = false) {
if ($options == false) {
$options = $this->options;
} else {
$this->options = $options;
}
$this->wsdlLocation = $wsdl;
$this->client = new SoapClient($wsdl, $options);
}
/*
* Executes a given function when supplied the proper function name,
* parameters and options.
*/
function call($function, $parameters, $options=NULL) {
$this->function = $function;
$this->parameters = $parameters;
try {
//$this->response = $this->client->__soapCall($function, $parameters, $options);
$this->response = $this->client->$function($parameters, $options);
} catch (SoapFault $exception) {
$this->$exception = $exception;
}
//get info about the last request
$this->request = $this->client->__getLastRequest();
$this->requestHeaders = $this->client->__getLastRequestHeaders();
//more info about the last responce
$this->responseHeaders = $this->client->__getLastResponseHeaders();
//set up an easily searchable array of results
$this->flatten();
return $this->response;
}
/*
* Prints all kinds of interesting info about what went on for debugging
* purposes
*/
function printInfo() {
echo '<h2>SoapClient Info:</h2>';
echo 'wsdl location: ' . $this->wsdl_location . '<br/>';
echo 'SoapClient Options:';
echoPre($this->options);
echo '<h2>Call Info:</h2>';
echo 'Function Name: ' . $this->function . '<br/>';
echo 'Parameters: ';
echoPre($this->parameters);
echo '<h2>Last Request: <br></h2>';
echo $this->format($this->request);
echo '<h2>Request Headers: <br></h2>';
echo $this->format($this->requestHeaders);
echo '<h2>Last Response: <br></h2>';
echoPre($this->response);
echo '<h2>Response Headers: <br></h2>';
echo $this->format($this->responseHeaders);
}
/*
* Formats the xml to make it nice and purdy for display and debugging
* purposes
*/
function format($xml) {
// add marker linefeeds to aid the pretty-tokeniser (adds a linefeed between all tag-end boundaries)
$xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $xml);
// now indent the tags
$token = strtok($xml, "\n");
$result = ''; // holds formatted version as it is built
$pad = 0; // initial indent
$matches = array(); // returns from preg_matches()
// scan each line and adjust indent based on opening/closing tags
while ($token !== false) :
// test for the various tag states
// 1. open and closing tags on same line - no change
if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) :
$indent = 0;
// 2. closing tag - outdent now
elseif (preg_match('/^<\/\w/', $token, $matches)) :
$pad--;
// 3. opening tag - don't pad this one, only subsequent tags
elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
$indent = 1;
// 4. no indentation needed
else :
$indent = 0;
endif;
// pad the line with the required number of leading spaces
$line = str_pad($token, strlen($token) + $pad, ' ', STR_PAD_LEFT);
$result .= $line . "\n"; // add to the cumulative result, with linefeed
$token = strtok("\n"); // get the next token
$pad += $indent; // update the pad size for subsequent lines
endwhile;
$result = highlight_string($result);
//nl2br(htmlentities($result));
return $result;
}
/*
* Searches the pre flattened array for a given key. If there is only one
* result, this will return a single value, if there are multiple results,
* it will return an array of values.
*
* @param string; $search - search for a response with this key
*/
function find($search=false) {
if ($search == false) {
return $this->flatArray;
} else {
if (isset($this->flatArray[$search])) {
$result = $this->flatArray[$search];
} else {
return false;
}
}
if(count($result)==1){
return $result[0];
}
else{
return $result;
}
}
/*
* This method flattens an array/object result into an array that is easy
* to search through. Search terms are set as keys with results set as
* arrays owned by said keys.
*/
function flatten($array=false) {
if ($array == false) {
$array = $this->response;
}
if (is_object($array)) {
//get the variables of object
$array = get_object_vars($array);
}
//howdy('array');
//echoPre($array);
//echo "_______________<br>";
if (is_array($array)) {
//loop through the sub elements and make sure they are arrays
foreach ($array as $key => $value) {
//if it's an object, we need to convert it to an array
if (is_object($value)) {
//get the variables of object
$value = get_object_vars($value);
}
//echo "key: $key value: ";
//echoPre($value);
//echo "_______________<br>";
//push the key=>value pairs to the flat array
if (!isset($this->flatArray[$key])) {
$this->flatArray[$key] = array();
}
array_push($this->flatArray[$key], $value);
if (is_array($value)) {
$this->flatten($value);
}
}
}
}
function getWSDL() {
$wsdl = file_get_contents($this->wsdlLocation);
}
}
可能重複[從其他文件的類函數變量](http://stackoverflow.com/questions/9363308/get-variable-from-other-files-class-function) – Gordon 2012-02-20 15:40:14
你甚至閱讀我的問題?這是關於閱讀一個變量中的XML文檔。儘可能遠離相關事物。 – OptimusCrime 2012-02-20 15:43:42
那又如何不重複呢?用SimpleXML加載變量,然後可以像顯示的那樣訪問它。除了如何解析和處理XML之外,SOAP或HTML經常被回答,還有另一個問題要求如何從XML文檔中獲取' '。在你的問題中沒有任何具體的東西來證明你沒有關閉它。請看右邊的相關部分。 –
Gordon
2012-02-20 15:54:49