如何将PHP STDCLASS对象转换为XML?

发布于 2025-01-19 15:36:42 字数 726 浏览 1 评论 0原文

我使用此代码连接到Web服务,该服务使我可以检索电子商务产品的库存。但是,后者返回一个stdclass对象(下图)。

$wsdl = "https://sinex-saas.com/blabla/Services/WebServiceStandard.asmx?WSDL";

$password = "????";
$username = "????";

$options = array(
    'user' => $username,
    'password' => $password,
);

$client = new SoapClient($wsdl);
$response = $client->RecupererEtatStock($options);
$presque = $response->RecupererEtatStockResult;

echo '<pre>';
print_r($presque);

我的目标是将此STDClass对象转换为XML,以便我可以将其用于WordPress插件。您知道如何使用PHP将stdclass转换为XML吗?

I use this code to connect to a web service that allows me to retrieve a stock of products for e-commerce. However, the latter returns a stdClass Object (photo below).

$wsdl = "https://sinex-saas.com/blabla/Services/WebServiceStandard.asmx?WSDL";

$password = "????";
$username = "????";

$options = array(
    'user' => $username,
    'password' => $password,
);

$client = new SoapClient($wsdl);
$response = $client->RecupererEtatStock($options);
$presque = $response->RecupererEtatStockResult;

echo '<pre>';
print_r($presque);

My goal is to convert this stdClass object to XML so that I can use it for a WordPress plugin. Do you know how to convert a stdClass to XML with PHP?

stdClassObject photo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2025-01-26 15:36:42

不要尝试构建通用转换。读取特定的对象结构并从中创建 DOM 会容易得多。这允许您定义结构、验证和重新格式化值、忽略不需要的值……

$data = getData();

$document = new DOMDocument();
// create and append a document element
$document->appendChild(
    $stock = $document->createElement('stock')
);

// iterate data array
foreach ($data->EtatStock as $stockData) {
    // create and append an element for each item
    $stock->appendChild(
        $item = $document->createElement('item')
    );
    // add a child element with text content
    $item
      ->appendChild($document->createElement('CodeMag'))
      ->textContent = $stockData->CodeMag;
    $item
      ->appendChild($document->createElement('LibMag'))
      ->textContent = $stockData->LibMag;
    // add value as an attribute 
    $item->setAttribute('quantity', $stockData->QteStock);
}

$document->formatOutput = true;
echo $document->saveXML();

function getData() {
    $json = <<<'JSON'
{
    "EtatStock": [
        {
            "CodeMag": "001",
            "LibMag": "SO SHOCKING",
            "QteStock": 2000
        }
    ]
}    
JSON;
    return json_decode($json);
}

输出:

<?xml version="1.0"?>
<stock>
  <item quantity="2000">
    <CodeMag>001</CodeMag>
    <LibMag>SO SHOCKING</LibMag>
  </item>
</stock>

Don't try to build a generic convert. It is a lot easier just to read a specific object structure and create a DOM from it. This allows you to define the structure, validate and reformat the values, ignore unneeded values, ...

$data = getData();

$document = new DOMDocument();
// create and append a document element
$document->appendChild(
    $stock = $document->createElement('stock')
);

// iterate data array
foreach ($data->EtatStock as $stockData) {
    // create and append an element for each item
    $stock->appendChild(
        $item = $document->createElement('item')
    );
    // add a child element with text content
    $item
      ->appendChild($document->createElement('CodeMag'))
      ->textContent = $stockData->CodeMag;
    $item
      ->appendChild($document->createElement('LibMag'))
      ->textContent = $stockData->LibMag;
    // add value as an attribute 
    $item->setAttribute('quantity', $stockData->QteStock);
}

$document->formatOutput = true;
echo $document->saveXML();

function getData() {
    $json = <<<'JSON'
{
    "EtatStock": [
        {
            "CodeMag": "001",
            "LibMag": "SO SHOCKING",
            "QteStock": 2000
        }
    ]
}    
JSON;
    return json_decode($json);
}

Output:

<?xml version="1.0"?>
<stock>
  <item quantity="2000">
    <CodeMag>001</CodeMag>
    <LibMag>SO SHOCKING</LibMag>
  </item>
</stock>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文