Zend Framework: catch custom soap exceptions - php

how do I catch my custom soap fault ProductoInexistente when requesting a soap web service operation? my code is the following, but it's not working:
$_WSDL_URI = 'http://joaquinlrobles.redirectme.net:8080/Pelopincho/PelopinchoService?WSDL';
$ws = new Zend_Soap_Client($_WSDL_URI, array('soap_version' => SOAP_1_1));
try {
$resultado = $ws->getStockProducto(array('idProducto' => $idProducto));
$this->view->resultado = $resultado->result;
}
catch (ProductoInexistente $ex) {
$this->view->resultado = 'Producto Inexistente';
}
thanks!

Is there the exception of type ProductoInexistente thrown?
Try changing the code to
$_WSDL_URI = 'http://joaquinlrobles.redirectme.net:8080/Pelopincho/PelopinchoService?WSDL';
$ws = new Zend_Soap_Client($_WSDL_URI, array('soap_version' => SOAP_1_1));
try {
$resultado = $ws->getStockProducto(array('idProducto' => $idProducto));
$this->view->resultado = $resultado->result;
}
catch (Exception $ex) {
var_dump($ex);
}
And see what's the name of exception class.
Unless the exception of ProductoInexistente it cannot be caught by catch(ProductoInexistente $ex)

Related

Catch Guzzle exceptions on connections refused

I am using Guzzle with the following code:
try {
$client = new Client();
$result = $client->get("http://{$request->ES_HOST}:{$request->ES_PORT}", [
'headers' => ['Content-Type' => 'application/json'],
'auth' => [$request->ES_LOGIN, $request->ES_PASSWORD],
'allow_redirects' => false,
]);
$response['status'] = $result->getStatusCode();
$response['message'] = json_decode($result->getBody()->getContents());
} catch (RequestException $e) {
$response['status'] = $e->getResponse()->getStatusCode();
$response['message'] = $e->getMessage();
}
And it works well however when an user gives the wrong URL for guzzle to process it instead of getting catched in RequestException it gives an 500 error in the server and returns a regular Exception with the message of cURL error 7: Failed to connect to [host] port [port]: Connection refused. Hoe can I make it so that I can catch the error and status code as well to return to the user?
Having tried your code, it seems to be throwing an instance of GuzzleHttp\Exception\ConnectException, so change the catch to
} catch (ConnectException $e) {
$response['status'] = 'Connect Exception';
$response['message'] = $e->getMessage();
}
( Adding the appropriate use statement...
use GuzzleHttp\Exception\ConnectException;
)
Also noticed that it doesn't have $e->getResponse()->getStatusCode(), which is why I've just set it to a fixed string.
Since Guzzle has so many Exceptions, i ended up checking which type of exception that guzzle has thrown and constructing a response accordingly:
try {
$client = new Client();
$result = $client->get("http://{$request->ES_HOST}:{$request->ES_PORT}", [
'headers' => ['Content-Type' => 'application/json'],
'auth' => [$request->ES_LOGIN, $request->ES_PASSWORD],
'allow_redirects' => false,
]);
$response['status'] = $result->getStatusCode();
$response['message'] = json_decode($result->getBody()->getContents());
} catch (ConnectException $e) {
$response['status'] = 404;
$response['message'] = $e->getMessage();
} catch (RequestException $e) {
$response['status'] = $e->getResponse()->getStatusCode();
$response['message'] = $e->getMessage();
} catch (\Exception $e) {
$response['status'] = 0;
$response['message'] = $e->getMessage();
}

php soap client issue

I have to develop a php client which connects to a soap webservice server.
With SoapUI, I can test the webservice which is working fine.
But from my php client, I get the following error:
syntax error near << from >>
Here is my php client code:
$wsdl = 'http://intrageo.cannes.fr:81/AdresseRecherche/searchByWhereClause?wsdl';
$trace = true;
$exceptions = false;
$xml_array['context'] = '?';
$xml_array['table'] = 'adr_digadr';
$xml_array['colonneARecuperer'] = 'numero';
$xml_array['clauseWere'] = 'nomvoie= \'BOULEVARD COINTET\'';
$xml_array['nbMaxLignes'] = 10;
try {
$client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
$response = $client->getDistinctValue($xml_array);
} catch (Exception $e) {
echo "Error!";
echo $e->getMessage();
echo 'Last response: '. $client->__getLastResponse();
}
var_dump($response);
Thanks for help.
It works if I use $response=$client->_soapCall("getDistinctValue",$xml_array); instead of $response=$client->getDistinctValue($xml_array)

php deleting files from google storage using google-api-php-client

Trying below code for deleting files from google storage but getting the error "An error occurred: (delete) missing required param: 'object'". I am sending the filesId like this $fileId = 1458180875815000.
$google-api-php-client :- getting the google service
// code for delete
$bucketName = "bucketname";
$googleServiceStorage = new Google_Service_Storage($this->client);
//$googleServiceStorage = new Google_Service_Storage_StorageObject();
try
{
$googleServiceStorage->objects->delete($bucketName,$companyId,
[
'object' => $companyId."/".$objectName,
'generation' => $fileId,
'alt' => "media"
]
);
//$bucketName,$companyId,$objectName,$fileId);
}
catch (Exception $e)
{
print "An error occurred: " . $e->getMessage();
}
}
Here is how I am deleting files from GCS
$bucket = 'my_main_bucket';
$file = 'path/to/file/image.jpg';
$this->service = new Google_Service_Storage($this->client);
try
{
$this->service->objects->delete($bucket, $file);
}
catch (Google_Service_Exception $e)
{
syslog(LOG_ERR, $e);
}

Refunding a captured authorization

am using the php api sdk found here: https://github.com/paypal/PayPal-PHP-SDK but am having trouble issuing a refund.
This is the code I am using:
// CAPTURE THE ORIGINAL AUTHORIZATION
try
{
$amount = new Amount();
$amount->setCurrency("USD");
$amount->setTotal($amount_authorized);
$capture = new Capture();
$capture->setId($authorization_id);
$capture->setAmount($amount);
$authorization = Authorization::get($transaction_id, $apiContext);
$capt = $authorization->capture($capture, $apiContext);
if ($capt->state == 'completed')
{
$capture_id = $capt->id;
}
}
catch (PayPal\Exception\PayPalConnectionException $e)
{
$response = json_decode($e->getData());
die('Error capturing funds: '.$response->message);
}
catch (Exception $e)
{
die('Error capturing funds: '.$e->getMessage());
}
// REFUND THE CAPTURE
try
{
$amount = new Amount();
$amount->setCurrency("USD");
$amount->setTotal($refund_amount);
$refund = new Refund();
$refund->setId($capture_id);
$refund->setAmount($amount);
$capture = Capture::get($capture_id, $apiContext);
$captureRefund = $capture->refund($refund, $apiContext);
if ($captureRefund->state == 'completed')
{
die('REFUNDED');
}
}
catch (PayPal\Exception\PayPalConnectionException $e)
{
$response = json_decode($e->getData());
die('There was an error refunding the funds: '.$response->message);
}
catch (Exception $e)
{
die('There was an error refunding the funds: '.$e->getMessage());
}
I get this error everytime: The requested resource ID was not found
I believe I am following the sample code on the developer site but I'll pulling my hair out. It's probably something pretty easy I hope.
Any ideas?
Thanks

php with soap service

i need to use this xml file:
http://www.mubashermisr.com/Mubadelayed/Service1.asmx?WSDL
to connect to GetTopGainers methode
i use this code:
<?php
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$wsdl_path = "http://www.mubashermisr.com/Mubadelayed/Service1.asmx?WSDL";
$parameters = array("Username"=>"xxxx","Password"=>"xxxx","ID"=>"xxxxx");
$client = new SoapClient($wsdl_path, array('trace' => 1));
try {
$result = $client->GetTopGainers($parameters);
print_r($result);
}
catch (SoapFault $exception) {
echo $exception;
}
?>
But i get the following error:
SoapFault exception: [soap:Server] Server was unable to process request.
---> Object reference not set to an instance of an object. in
C:\wamp\www\soap\soap.php:24 Stack trace: #0
C:\wamp\www\soap\soap.php(24): SoapClient->__call('GetTopGainers', Array)
#1 C:\wamp\www\soap\soap.php(24): SoapClient->GetTopGainers(Array) #2
{main}
Any help will be appreciated.
Thanks
<?php
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$wsdl_path = "URL";
$parameters = array("Username"=>"xxx","Password"=>"xxx","ID"=>"xxx");
$header = new SOAPHeader('Namespace', 'Auth', $parameters);
$client = new SoapClient($wsdl_path);
$client->__setSoapHeaders($header);
try {
$topGainers = $client->GetTopGainers(); // Top Gainers
echo"<pre>";
print_r($topGainers);
echo"</pre>";
}
catch (SoapFault $exception) {
echo $exception;
}
?>

Categories