1
我想解決以下問題:PHP:處理API請求的未知結果
我打電話一個SOAP API,並嘗試處理答案的方式,讓我以確定是否有對象存在在目標服務器上或沒有。我有ContactGet
,CustomerGet
和UserGet
。
如果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
,CustomerGet
或UserGet
爲空,我該如何返回「真」或「假」?這些API調用的返回是不同的,所以我不知道如何處理它。
我試圖通過訪問所述第一對象與
count(current((Array)$result)
要解決的問題但是,這始終是INT(1)。
請出示posative成果,以及爲負。答案將是幾個if語句,調用'is_bool','empty'等。你自己嘗試過什麼嗎? – Steve
感謝您的回覆。我現在更新了我的問題。 – MyFault
我相信我的回答會有幫助。只需與一個函數分開處理每個「錯誤」調用並將其統一在您的代碼上即可 – Elentriel