Skip to content
Advertisement

Separate received from web-service as object std class [closed]

I got this from web service and tried so many solution that I found in similar topics, but couldn’t separate them to use in my code:

stdClass Object (
    [getPropsListResult] => [
        {
            "id":16461,
            "Material":"1000001",
            "SalesDescription":"product1",
            "Plnt":"1339",
            "PlantName":"WAREHOUSE1",
            "SalesPrice":"15000"
        },
        {
            "id":16462,
            "Material":"1000001",
            "SalesDescription":" product2",
            "Plnt":"1018",
            "PlantName":"WAREHOUSE2",
            "SalesPrice":"15000"
        },
        {
            "id":16463,
            "Material":"1000002",
            "SalesDescription":" product3",
            "Plnt":"1339",
            "PlantName":"WAREHOUSE1",
            "SalesPrice":"22000"
        },
        {
            "id":32920,
            "Material":"1072941",
            "SalesDescription":"product4",
            "Plnt":"1018",
            "PlantName":" WAREHOUSE1",
            "SalesPrice":"0"
        }
    ]
)

Please help me how can I use these information?

Advertisement

Answer

The response that you received from the web service has json encoded payload. To use that pay load you will require to json_decode it first.

Here is how you can loop over the result items to access each individual item in the list.

$itemList = json_decode($webServiceResponse->getPropsListResult);
foreach ($itemList as $item) {
   // access props like this
   echo $item->id;
   echo $item->Material;
   echo $item->SalesDescription;
   // and so on...
}

Try using the code like this:

$soapclient = new SoapClient('http://****.com/Pages/WebService/Keramat/KeramatWebService.asmx?wsdl');
try {
    $response = $soapclient->getCanboPropsList();
    $itemList = json_decode($response->getPropsListResult);
    foreach ($itemList as $item) {
       // access props like this
       echo $item->id;
       echo $item->Material;
       echo $item->SalesDescription;
       // and so on...
    }
}
catch(Exception $e) {
    echo ($soapclient->__getLastResponse());
    echo PHP_EOL;
    echo ($soapclient->__getLastRequest());
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement