【问题标题】:Separate received from web-service as object std class [closed]作为对象标准类从 Web 服务中单独接收 [关闭]
【发布时间】:2020-04-16 04:47:43
【问题描述】:

我从网络服务中得到了这个,并尝试了很多我在类似主题中找到的解决方案,但无法将它们分开以在我的代码中使用:

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"
        }
    ]
)

请帮助我如何使用这些信息?

【问题讨论】:

  • 你不能separate他们是什么意思?
  • 我需要知道每个产品的 id、名称和 salesprice 以插入到我的数据库中。
  • 您可以将 Web 服务响应分配给一个变量。让我们称之为$result。然后你可以在$result->getPropsListResult数组上做一个循环

标签: php stdclass


【解决方案1】:

您从 Web 服务收到的响应包含 json encoded 有效负载。要使用该有效负载,您需要先json_decode

您可以通过以下方式遍历结果项以访问列表中的每个单独项。

$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...
}

尝试使用如下代码:

$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());
}

【讨论】:

  • 这段代码没有显示任何内容。
  • 什么print_r 语句创建了您在问题中显示的输出?
  • print_r($itemList);什么都不显示。
  • ok 尝试运行更新后的答案中显示的代码
  • 我试过了,但什么也没显示
【解决方案2】:

你也许可以这样做:

$result = <webservice response>;

foreach ($result->getPropsListResult as $prop) {
   $prop['SalesPrice']; // To get salesprice
   $prop['PlantName']; // To get plant name
}

【讨论】:

  • 我试过这段代码,但什么也没显示: $soapclient = new SoapClient('http://****.com/Pages/WebService/Keramat/KeramatWebService.asmx?wsdl');试试 { $response = $soapclient->getCanboPropsList(); } catch (Exception $e) { echo($soapclient->__getLastResponse());回声 PHP_EOL;回声($soapclient->__getLastRequest()); } foreach ($response->getPropsListResult as $prop) { echo $prop['SalesPrice']; // 获取salesprice echo "
    "; $prop['植物名称']; // 获取植物名称 echo "

    "; }
  • 对,所以它来自 SOAP Web 服务。你能print_r($response)看看你得到了什么吗?
  • 当我 print_r($response) 时,我得到了我提到的问题:stdClass Object ( [getPropsListResult] => [ { "id":16461, "Material":"1000001", .. ......
猜你喜欢
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多