I am interested in making a soap call via php’s soapClient to a web service to get the water level from a monitoring station. I want to handle two soapfaults that have occured during the execution. The first fault is as follows :
SoapFault exception: [soapenv:Server.userException] java.rmi.RemoteException: We are sorry, but no data is available from this station at this time in C:\xampp\htdocs\NOAA\LogWriter.php:214 Stack trace: #0 C:\xampp\htdocs\NOAA\LogWriter.php(214): SoapClient->__soapCall('getWaterLevelRa...', Array, Array) #1 C:\xampp\htdocs\NOAA\LogWriter.php(188): getLevel('8531680', '20120726 15:19') #2 {main}
This error is expected to occur several times during the script if the data for a certain time is not available. I need to catch this fault in order to tell the script to try again with a new time. I used a catch block to do so.
I also need to catch a second fault that occurs if the webservice is not loading the wsdl file or the server is timedout. To test for this have gave my script a faultly location to generate the same error I had received previously and it is as follows:
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://opendap.co-ops.nos.noaa.gov/axis/services/WaterLevelRawOneMin?wsdl' : Extra content at the end of the document in C:\xampp\htdocs\NOAA\LogWriter.php:210 Stack trace: #0 C:\xampp\htdocs\NOAA\LogWriter.php(210): SoapClient->SoapClient('http://opendap....', Array) #1 C:\xampp\htdocs\NOAA\LogWriter.php(171): getLevel('8531680', '20120726 12:35') #2 {main} thrown in C:\xampp\htdocs\NOAA\LogWriter.php on line 210
The second error remains uncaught and terminates my script. However I need to catch it and display a message.
I have posted my php function that makes the soap call below.
Could anyone give me any ideas on how to do this?
function getLevel($id, $date) {
$client = new SoapClient("http://opendap.co-ops.nos.noaa.gov/axis/services/WaterLevelRawOneMin?wsdl", array('trace' => false));
$Parameters = array("stationId" => $id, "beginDate" => $date, "endDate" => $date, "datum" => "MLLW",
"unit" => 1, "timeZone" => 1);
try {
return $client->__soapCall(
"getWaterLevelRawOneMin", array('Parameters' => $Parameters),
array('location' => "http://opendap.co-ops.nos.noaa.gov/axis/services/WaterLevelRawOneMin")
);
} catch (SoapFault $e) {
if (
$e->faultcode == "soapenv:Server.userException"
and $e->faultstring == "java.rmi.RemoteException: We are sorry, but no data is available from this station at this time"
) {
return "FAULT";
} else {
echo "Could not connect to the server";
}
} // end of catch blocK
}// end of function
Exception regarding broken WSDL can occur only when you call SoapClient::constructor so
try {
$client= new SoapClient($wsdlUrl ,array('trace'=>false));
}catch(Exception $e) {
// your loging regarding this case
}
SoapFault exception can occur when you make a webservice all so:
try {
$client= new SoapClient($wsdlUrl ,array('trace'=>false));
try {
return $client->_call('....');
} catch (SoapFault $sp) {
//your logic rearding soap fault
}
}catch(Exception $e) {
// your loging regarding this case
}
return false;
Related
This is my code:
$this->options = [
"login" => $username,
"password" => $password
];
try {
$this->request = new SoapClient('https://servis.turkiye.gov.tr/services/g2g/kdgm/test/uetdsesya?wsdl', $this->options);
} catch(Exception $e) {
throw new Exception($e->getMessage());
}
And this is the error message:
Fatal error: Uncaught Exception: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://servis.turkiye.gov.tr/services/g2g/kdgm/test/uetdsesya?wsdl' : failed to load external entity "https://servis.turkiye.gov.tr/services/g2g/kdgm/test/uetdsesya?wsdl"
Any idea how I can fix this error?
[UPDATE]
I can get functions with this code:
$wsdl = file_get_contents('https://servis.turkiye.gov.tr/services/g2g/kdgm/test/uetdsesya?wsdl');
But this code still gets error:
SoapClient('https://servis.turkiye.gov.tr/services/g2g/kdgm/test/uetdsesya?wsdl', $this->options);
try from command line:
wget https://servis.turkiye.gov.tr/services/g2g/kdgm/test/uetdsesya?wsdl
or
php -r '$a = new SoapClient("https://servis.turkiye.gov.tr/services/g2g/kdgm/test/uetdsesya?wsdl"); var_dump($a);'
Maybe resource id not available from tour host
To handle SOAP exceptions, you can give this answer a try. Following code is standalone code which is returning a SOAP response.
$params = array(
"login" => "USERNAME",
"password" => "PASSWORD"
);
try {
$client = new SoapClient('https://servis.turkiye.gov.tr/services/g2g/kdgm/test/uetdsesya?wsdl', array($params));
} catch(Exception $e) {
throw new Exception($e->getMessage());
}
To List all available SOAP functions:
echo " List of available SOAP functions <br/><pre>"; var_dump($client->__getFunctions()); echo "</pre>";
To List all available SOAP types:
echo "List of SOAP types <br/><pre>"; var_dump($client->__getTypes()); echo "</pre>";
This answer has an explanation about how to access any available SOAP function.
I am new to SOAP API and WSDL. I had this project to use the SOAP API and I used this code from one of the stackoverflow answers. But when I am running this, I am getting this error.
Fatal error: Uncaught SoapFault exception: [HTTP] Forbidden in 'directory' Stack trace: #0 [internal function]: SoapClient->__doRequest('...') #1 directory): SoapClient->__call('method', Array) #2
Here is my code:
<?php
try{
$wsdl_url = 'API';
$client = new SoapClient($wsdl_url);
} catch (SoapFault $e) {
echo 'Error in Soap Connection : '.$e->getMessage();
}
$params = array(
'LBID' => '1',
'YearID' => '23',
'SectorId' => '3',
'Password' => 'password'
);
$result= $client->getProjectDetails($params);
print_r($result);
?>
I am able to access the api by directly putting it on address bar. The soap connection is established, no errors there. But this error is showing on the method calling line, $result= $client->getProjectDetails($params);
I don't know what this error is, I tried searching the forums but couldn't get any idea about this. Can anyone help?
Im new to Five9 i got some sample code in GitHub (https://github.com/kielerrr/Five9 ) which i went though the code and i passed the credentials into API but i get the following error
"Fatal error: Uncaught Error: Class 'SoapClient' not found in C:\xampp\htdocs\Five9-master\includes\Five9.php:20 Stack trace: #0 C:\xampp\htdocs\Five9-master\contacts\getContactRecords.php(12): f9->__construct() #1 {main} thrown in C:\xampp\htdocs\Five9-master\includes\Five9.php on line 20"
$wsdl_five9 = "https://api.five9.com/wsadmin/v4/AdminWebService?wsdl&user=User_ID";
//$wsdl_five9 = "https://api.five9.com/wsadmin/v4/AdminWebService?wsdl";
try {
$soap_options = array('login' => 'uer_name', 'password' => 'mypassword', 'trace' => true);
$this->_connection = new SoapClient($wsdl_five9, $soap_options);//20th line
} catch (Exception $e) {
$error_message = $e->getMessage();
echo $error_message."ERROR";
exit;
}
Please help me to go through this ...
The Above code works fine need to enable soap in your system .
locate your php.ini file and Un-comment(remove semicolon before soap extension)as below.
change from :
";extension=php_soap.dl"
to :
"extension=php_soap.dll" .
I want to know what is the original exception details in my SOAP code, I have a SOAP server that handles requests as the following:
$options = array(
'soap_version' => SOAP_1_2,
'actor' => someUriAString,
'encoding' => 'UTF-8',
'uri' => someUriAString);
$server = new Server(null, $options);
$server->setClass('SomeClass');
$server->setReturnResponse(true);
$serverResponse = $server->handle();
and then I check if an exception occurs as the following:
if ($serverResponse instanceof \SoapFault) {
//log the $serverResponse exception details
}
but when I log this exception I got something like this:
exception 'Exception' with message 'SoapFault exception: [Receiver] Unknown error
the thing I need to know is the original exception details... like SQL exception, or for example ORMException,...etc. i.e. I need the exact original exception details...
I already tried to registerFaultException as following example:
$server->registerFaultException('Doctrine\ORM\ORMException');
I don't know if this is right, but the problem is that there may occur other types of exception, I can not register them since I don't know what exception could occur in my code!
It depends how the expections are setup, but you can get Previous exception message:
$message->getPrevious();
You can iterate overthem like this:
if($message instanceof \Exception) {
do {
echo sprintf(
"%s:%d %s (%d) [%s]\n",
$message->getFile(),
$message->getLine(),
$message->getMessage(),
$message->getCode(),
get_class($message)
);
}
while($message = $message->getPrevious());
}
I'm using the Facebook PHP API, and around 1 time in 40 it dumps this exception on my webapp:
Uncaught CurlException: 56: SSL read:
error:00000000:lib(0):func(0):reason(0),
errno 104 thrown in ... on line 638
I'm not looking for a solution to what's causing the exception (already working on that), but for now I'd like to change it from dumping the exception on the page to either telling the user to refresh the page or refreshing the page automatically.
The exception is being thrown in this file: https://github.com/facebook/php-sdk/blob/master/src/facebook.php
This is the code I'd like to temporarily change to a refresh / refresh instruction:
if (curl_errno($ch) == 60) { // CURLE_SSL_CACERT
self::errorLog('Invalid or no certificate authority found, using bundled information');
curl_setopt($ch, CURLOPT_CAINFO,
dirname(__FILE__) . '/fb_ca_chain_bundle.crt');
$result = curl_exec($ch);
}
if ($result === false) {
$e = new FacebookApiException(array(
'error_code' => curl_errno($ch),
'error' => array(
'message' => curl_error($ch),
'type' => 'CurlException',
),
));
curl_close($ch);
throw $e;
}
You could use TRY .. CATCH to catch CurlException, or FacebookApiException there.
Or use set_exception_handler to catch any uncaught exception.
As strauberry said, you can stop throwing the exception. If the exception needs to be throw there, you can put the code that call this inside a try-catch and deal with the exception as you want.
Another option is to use the function set_exception_handler. This function will be called when an exception is thrown but nothing catch it.
You throw the Exception $e in the last line which causes the dumping. Instead you could do something like
echo "ERROR: " . $e->getMessage();