【问题标题】:prestashop webservice : using PHP to make a CRUD application, issue retrieving informations on an XMLprestashop webservice : 使用 PHP 制作 CRUD 应用程序,在 XML 上检索信息
【发布时间】:2013-01-30 04:11:50
【问题描述】:

美好的一天,我正在 prestashop 上启动一个 CRUD 应用程序,我想通过管理存储的程序启动的 php 应用程序使用 web 服务来更新、创建或删除产品、用户和订单。

我看过 prestashop 指南: http://doc.prestashop.com/display/PS15/Using+the+PrestaShop+Web+Service

现在我正在努力完成任务。

我实际上收到了一个代表产品结构的 xml:

<?php
// Here we define constants /!\ You need to replace this parameters
define('DEBUG', true);
define('PS_SHOP_PATH', 'http://www.server.com/prestashop/');
define('PS_WS_AUTH_KEY', 'blahblahbla');
require_once('./PSWebServiceLibrary.php');




    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
    $opt = array('resource' => 'products');
    $opt['id'] =1;

        $xml = $webService->get($opt);
        $resources = $xml->children()->children();



foreach ($resources as $nodeKey => $node)
    {

        echo $nodeKey . " : ". $resources->$nodeKey ."<br>";
    }


/*$opt = array('resource' => 'products');
$opt['putXml'] = $xml->asXML();
$opt['id'] = 1;
$xml = $webService->edit($opt);*/


?>

我遇到的第一个问题:

“名称”字段在 xml 中有一些子项,它们是不同的语言,因此,在我浏览节点的循环中,我想“进入”那些有孩子的……因为实际上我尝试“echo”的名称是空的,但如果我看到 xml,我可以在 &lt;name&gt; 下看到 2 个节点,其中包含 2 种不同语言的名称。 暂时就这些了,以后我会发布越来越多的问题:D

提前致谢!

【问题讨论】:

标签: php xml web-services crud prestashop


【解决方案1】:

您正在引用具有子 (http://php.net/manual/en/class.simplexmlelement.php) 的 SimpleXMLElement。在这种情况下,它有子命名语言。

Array
(
    [name] => SimpleXMLElement Object
        (
            [language] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 1
                        )

                )

        )

)

因此您可以像下面这样访问。

if($resources->$nodeKey->children()){

    $children = false;
    foreach($resources->$nodeKey->children() as $child){
        $children[] = $child;
    }

    echo $nodeKey . " : [" . @implode(", ", $children) . "]<br>";
}
else    
    echo $nodeKey . " : ". $resources->$nodeKey ."<br>";

结果:

Array
(
    [name] => SimpleXMLElement Object
        (
            [language] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 1
                        )

                )

        )

)
name : [iPod Nano]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多