3

I have this weird situation where SOAP retuns no value but I am not sure how to check whether it has returned any value or not.

For example, I am calling SOAP to get product image detail like this:

$result = $this->proxy->call($this->sopaSession, 'catalog_product_attribute_media.list', $sku);

For one SKU, I have images uploaded and it returns the correct values into $result. All fine, all happy!

But for another SKU, I do not have any images uploaded. So, upon calling aforementioned statement for this SKU, I am not sure what is returned - null? false? empty string?

if ($result){

    return $result;
} 

or

       if (isset(result)){

            return $result;
        }

or

       if ($result === null){

            return $result;
        }

None of these really work.

I am relatively new to PHP, Magento and SOAP.

Please guide me how can I check whether my SOAP call has returned any value or not. Based if a product has image or not, I want to do some further processing.

Appreciate any kind of hint/help.

Thank you! Dhananjay

3
  • Was any solution found? Dec 1, 2015 at 20:57
  • @AlexP very sorry for the delay, lot of work. But thank you very much for the hint on API returning array() always. However, for some reason, when there was no image for a product, API threw some other exception and not "SOAP Fault" exception. So, I could only catch it with catch (Exception $ex) { return false; } . Is that normal? Also, as per your suggestion, I made sure to return only Boolean from my function.. Dec 4, 2015 at 3:15
  • Your solution sounds good. It is strange though that something other than SoapFault is thrown. Dec 4, 2015 at 8:01

1 Answer 1

2

According to implementation of this API (Mage_Catalog_Model_Product_Attribute_Media_Api::items()), result is always an array. So if ($result) should return true only in case when product has images.

Also make sure to wrap the code into try-catch block and process SoapFault exceptions. For example in case when specified product does not exist.

2
  • Hello @alex Thank you for your response. I now added the SOAP_Fault try catch block. Below is my code - public function getImageInfo($sku){ $result = null; try { $result = $this->proxy->call($this->sopaSession, 'catalog_product_attribute_media.list', $sku); } catch (SOAP_Fault $ex){ return $ex->getMessage(); } if (isset($result)){ return $result; } return "no image exists"; } What I expect this code to do is - Return the image details as $result array if there are images, if not, return "no image exists". But it doesn't seem to work that way.. Nov 26, 2015 at 5:59
  • isset($result) will always be true in your case, leave just if($result). Also it would be better if your method always return an array type. Nov 26, 2015 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.