Home > .NET, PHP > Get JSON data from web service with PHP

Get JSON data from web service with PHP

2013/09/18

In this case there is an .asmx web service that has a Login method, if the result is True (user has written correctly user and password) we can use other methods.

We use a GetHistoryTrucks method that returns a JSON string, the raw output is:

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http:/www.mysite.com/">
[{"GTI_DIV_IDLoc":"1","GTI_DIV_ID_Sector":"888","GTI_DIV_ID_Vehicle":"T0255","GTI_DIV_COMPANY":" "},   
 {"GTI_DIV_IDLoc":"2","GTI_DIV_ID_Sector":"888","GTI_DIV_ID_Vehicle":"T1717","GTI_DIV_COMPANY":"B"}]
</string>

and this is a sample how to reads the couples key value from PHP:

<?php
 
$wsdl_address = "http://192.168.0.7:8078/services/autobahn.asmx?WSDL";
$client = new SoapClient($wsdl_address, array("trace" => 1, "exceptions" => 0));
$params = array("user"=>"smith", "password"=>"somepwd");
$response = $client->Login($params);
if ($response)
{
    $data = $client->GetHistoryTrucks();
    $row =json_decode($data->GetHistoryTrucksResult,true);
    for ($i = 0; $i < count($row); ++$i) {
        echo "<table border = 1>";
        echo "<tr>";
        echo "<td>Key</td>";
        echo "<td>Value</td>";
        echo "</tr>";
        foreach($row[$i] as $key => $value)
        {
            echo "<tr>";
            echo "<td>".$key."</td>";
            echo "<td>".$value."</td>";
            &nbsp ; echo "</tr>";
            //echo $key.": ".$value."<br />";
        }
    }
    echo "</table>";
}
else {
    echo "not authorized";
}
 
?>

The final output:

Key

Value

GTI_DIV_IDLoc

1

GTI_DIV_ID_Sector

888

GTI_DIV_ID_Vehicle

T0255

GTI_DIV_COMPANY

 

Key

Value

GTI_DIV_IDLoc

2

GTI_DIV_ID_Sector

888

GTI_DIV_ID_Vehicle

T1717

GTI_DIV_COMPANY

B

The raw stream can be readed with

$row = json_decode($data->GetHistoryTrucksResult->any['text'], true);
Advertisements
Categories: .NET, PHP
%d bloggers like this: