2015-11-23 73 views
1

我想解決以下問題:PHP:處理API請求的未知結果

我打電話一個SOAP API,並嘗試處理答案的方式,讓我以確定是否有對象存在在目標服務器上或沒有。我有ContactGet,CustomerGetUserGet

如果ContactGet聯繫人的答案是這樣的匹配:

object(stdClass)#394 (12) { ["type"]=> int(0) ["salutation"]=> int(0) ["firstname"]=> string(4) "Test" ["lastname"]=> string(4) "Test" ["address1"]=> string(12) "Teststreet 3" ["zipcode"]=> string(5) "12345" ["city"]=> string(8) "Testcity" ["country"]=> string(2) "DE" ["phone"]=> string(14) "+4912345678910" ["fax"]=> string(14) "+ 4912345678911" ["mobile"]=> string(14) "+ 4912345678913" ["email"]=> string(16) "[email protected]" } 

如果ContactGet的答案是這樣的:

bool(false) 

如果我使用CustomerGet並具有正匹配輸出如下:

object(stdClass)#401 (1) { ["CustomerDetails"]=> object(stdClass)#387 (6) { ["id"]=> string(12) "cB-pUbAED.ca" ["cid"]=> int(76) ["owner_c"]=> string(12) "cL2AmcgAdSMa" ["admin_c"]=> string(12) "cL2AmcgAdSMa" ["billing_c"]=> string(12) "cL2AmcgAdSMa" ["locked"]=> int(0) } } 

如果customer不存在答案是:

object(stdClass)#398 (1) { ["customers"]=> object(stdClass)#401 (0) { } } 

同樣是UserGet

object(stdClass)#409 (1) { ["users"]=> object(stdClass)#412 (0) { } } 

意味着沒有這樣的用戶。

現在我想有不關心我檢查哪種類型的手柄的功能:

的函數調用目標服務器上的getXXXX功能並返回結果。

我的問題是:如果我不知道我得到的回報是什麼,如果ContactGet,CustomerGetUserGet爲空,我該如何返回「真」或「假」?這些API調用的返回是不同的,所以我不知道如何處理它。

我試圖通過訪問所述第一對象與

count(current((Array)$result) 

要解決的問題但是,這始終是INT(1)。

+0

請出示posative成果,以及爲負。答案將是幾個if語句,調用'is_bool','empty'等。你自己嘗試過什麼嗎? – Steve

+0

感謝您的回覆。我現在更新了我的問題。 – MyFault

+0

我相信我的回答會有幫助。只需與一個函數分開處理每個「錯誤」調用並將其統一在您的代碼上即可 – Elentriel

回答

0

錯誤時返回true,錯誤時返回false;

is_error($message) 
{ 
if(empty($message)) return true; 
if(isset($message->customers) && empty($message->customers)) return true; 
if(isset($message->users) && empty($message->users)) return true; 
return false; 
} 

而且使用這樣的:

$message = call_to_get_message(); 
if(is_error($message)) 
{ 
//handle the error 
} 
else 
{ 
//proceed 
}