在浏览器中显示部分输出

时间:2018-08-03 09:20:58

标签: php

背景

我正在拨打 Soap Url ,并根据 ShipmentNumber

在浏览器中获取消息

案例1 :当我在浏览器中运行代码时,对于某些 ShipmentNumber ,我得到以下结果作为输出:

stdClass Object
(
    [Transaction] => stdClass Object
        (
            [Reference1] => 10000128254545
            [Reference2] => 
            [Reference3] => 
            [Reference4] => 
            [Reference5] => 
        )

    [Notifications] => stdClass Object
        (
        )

    [HasErrors] => 1
    [ProcessedShipmentHolds] => stdClass Object
        (
            [ProcessedShipmentHold] => stdClass Object
                (
                    [ID] => 42863418581
                    [HasErrors] => 1
                    [Notifications] => stdClass Object
                        (
                            [Notification] => stdClass Object
                                (
                                    [Code] => ERR65
                                    [Message] => Hold of the same type already exists
                                )

                        )

                )

        )

)

案例2 :对于其他一些 ShipmentNumber ,我得到的结果是:

stdClass Object
(
    [Transaction] => stdClass Object
        (
            [Reference1] => 10000128254545
            [Reference2] => 
            [Reference3] => 
            [Reference4] => 
            [Reference5] => 
        )

    [Notifications] => stdClass Object
        (
        )

    [HasErrors] => 
    [ProcessedShipmentHolds] => stdClass Object
        (
            [ProcessedShipmentHold] => stdClass Object
                (
                    [ID] => 42863421156
                    [HasErrors] => 
                    [Notifications] => stdClass Object
                        (
                        )

                )

        )

)

要求

但是我只想在浏览器中显示 [消息] 值。意味着,在情况1中,我想显示“ 相同类型的夹持器已经存在”,在情况2中,我要在浏览器中显示“ 没有消息”。 ..

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    $soapClient = new SoapClient('https://ws.aramex.net/ShippingAPI.V2/Shipping/Service_1_0.svc?wsdl');

    $params = array(
            'ClientInfo'            => array('AccountNumber' => 'IN',                                   
                                    'Version' => 'v1.0'
                                ),

            'Transaction'           => array(
                                        'Reference1'        => '',                                      
                                        'Reference5'        => ''                               
                                    ),

            'ShipmentHolds'=> array(

                            'ShipmentHoldDetails' => array(
                                            'ShipmentNumber' =>'42863421156',
                                            'Comment' =>'test Order'
                                            )
                                            )

    );


    try {
        $auth_call = $soapClient->HoldShipments($params);
        echo '<pre>';
        print_r($auth_call);
        die();
    } catch (SoapFault $fault) {
        die('Error : ' . $fault->faultstring);
    }
?>

1 个答案:

答案 0 :(得分:1)

尝试此代码。您需要访问右侧字段以显示消息。

您的消息在:

$auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message

但是根据您的第二个问题,有时您可能没有答案,因此可以尝试以下操作:

    if(isset($auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message)){
     echo $auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message;
    }
     else{
     echo "No Message";
    }

您可以使其更“优雅”,并使用

之类的三元运算符
isset($auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message)?$Message=$auth_call->ProcessedShipmentHolds->ProcessedShipmentHold->Notifications->Notification->Message:$Message='No Message';

echo $Message;