We’re going through some code written by a contractor a couple yrs ago and adding better exception handling. This is an example of several functions that make SOAP calls to our webservice.
BTW, this code sometimes throws the error: Undefined index: result…Line 137 (marked below)
function ParticipantSelectByUser() {
$param = array();
$param["Username"] = $_POST["Username"];
$param["Password"] = $_POST["Password"];
$result = $this->client->call("ParticipantSelectByUser",array("parameters" => $param),"http://tempuri.org/");
if ($this->client->fault) {
$this->return_data["error"] .= print_r($result, true);
} else {
// Check for errors
$err = $this->client->getError();
if ($err) {
// Display the error
$this->return_data["error"] .= $err;
$this->return_data["error"] .= print_r($this->client->response, true);
return json_encode($this->return_data);
} else {
$this->return_data["result"] = $result["ParticipantSelectByUserResult"];
} // if ($err)
} // if ($this->client->fault)
return json_encode(array("result" => $this->return_data["result"])); // Line 137
} // function ParticipantSelectByUser()
Does this code below do a better job at handling error conditions than the original code above?
function ParticipantSelectByUser() {
$param = array();
$param["Username"] = $_POST["Username"];
$param["Password"] = $_POST["Password"];
try {
$result = $this->client->call("ParticipantSelectByUser",array("parameters" => $param), "http://tempuri.org/");
if ($this->client->fault) {
throw new Exception(print_r($result, true));
} else {
// Check for errors
$err = $this->client->getError();
if ($err) {
// Display the error
$this->return_data["error"] .= $err;
$this->return_data["error"] .= print_r($this->client->response, true);
throw new Exception(json_encode($this->return_data));
} // if ($err)
} // if ($this->client->fault)
} catch (Exception $e) {
return json_encode($e);
} // try..catch
$this->return_data["result"] = $result["ParticipantSelectByUserResult"];
return json_encode(array("result" => $this->return_data["result"]));
} // function ParticipantSelectByUser()