【问题标题】:Trouble with parsing XML data from cURL POST in PHP在 PHP 中解析来自 cURL POST 的 XML 数据时遇到问题
【发布时间】:2018-07-23 05:22:13
【问题描述】:

我正在努力将目录功能集成到我设计的公司网站akserigraphics.com。我可以从 cURL POST 获得响应,但似乎无法正确格式化以在 .php 页面上创建目录视图。

Here 是我试图在 PHP 中填充 XML 数据的布局。下面是我正在使用的代码,下面是我收到的输出。

代码

<?php
    $url = 'https://www.promoplace.com/ws/ws.dll/XMLDataStream';
    $xml_input = '<?xml version="1.0" encoding="utf-8"?>
        <XMLDataStreamRequest>
        <Ver>3.2</Ver>
        <Auth>
            <AcctID>xxx</AcctID>
            <LoginID>yyy</LoginID>
            <Password>zzz</Password>
        </Auth>
        <Search>
            <Category>Shirts</Category> <SPC>S:50042,S:50316,S:50609,S:63196,S:64202,S:69820,S:69813,S:69783,S:50007,S:50066,S:50673,S:51080,S:51118,S:51625,S:67821,S:62358,S:67591,S:66971,S:63758,S:65046,S:66312,S:67336,S:67466,S:68866</SPC>
            <QuickSearch>t-shirt</QuickSearch>
            <Sort>POPULARITY</Sort>
            <StartNum>1</StartNum>
            <MaxRecs>12</MaxRecs>
            <MaxTotalItems>2500</MaxTotalItems>
            <ExtraReturnFields>ITEMNUM</ExtraReturnFields>
        </Search>
        </XMLDataStreamRequest>';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_input);

    if(curl_errno($ch)) {
        echo curl_errno($ch);
        echo curl_error($ch);
    }
    else {
        $parsed = array();
        parse_str(curl_exec($ch), $parsed);
        print_r($parsed);
        curl_close($ch);
        foreach ($parsed as $value => $item): ?>
            <a href="/products/garments/t-shirts/productview/">
              <div id="shirt_<?php $item->Count; ?>'" class="portfolio-box-1 three-col-por">
                <div class="mask"></div>
                <h3><span><?php $item->ItemNum; ?></span><br><?php $item->PrName; ?></h3>
			          <img src="<?php $item->ThumbPicLink; ?>" alt="<?php $item->PrName; ?>">
              </div>
			      </a>
        <?php endforeach;        
    }
?>

输出

数组 ( [ "1.0" encoding="UTF-8"?>
3.2 根据您的协议条款使用。未经授权禁止使用。供应商信息是保密的。 (C) 2018 QUICK TECHNOLOGIES INC. 1 1000 1 343212964 HVKQF-HAUXO
端口 [amp;_Company®_5_4_Oz__100_percent_Cotton_Tee_Shirt PC54 5_02_-_7_02 http://www_promoplace_com/ws/ws_dll/QPic?SN] =>
50042 [amp;P] => 122090667 [amp;RS] =>
150 [amp;_Company®_Core_Blend_Short_Sleeve_T 恤 PC55 5_62_-_7_62 http://www_promoplace_com/ws/ws_dll/QPic?SN] =>
50042 [amp;_Company®_Essential_T 恤 PC61 5_96_-_7_96 http://www_promoplace_com/ws/ws_dll/QPic?SN] => 50042 )

我已经在互联网上搜索了好几天,似乎无法确定我做错了什么。我只是 PHP 的初学者,因此我们将不胜感激。

【问题讨论】:

    标签: php xml html curl post


    【解决方案1】:

    希望这会有所帮助。您可能还想从此请求中删除您的 API 凭据?如果此 API 仅限于您的使用?我已将它们从下面的答案中删除并替换为####。此外,我正在使用 PHP 的 simplexml_load_string 将响应放入对象中以进行迭代。我在本地对此进行了测试,并且有效。如果您有任何问题,请告诉我?

    <?php
        $url = 'https://www.promoplace.com/ws/ws.dll/XMLDataStream';
        $xml_input = '<?xml version="1.0" encoding="utf-8"?>
            <XMLDataStreamRequest>
            <Ver>3.2</Ver>
            <Auth>
                <AcctID>#####</AcctID>
                <LoginID>#####</LoginID>
                <Password>#####</Password>
            </Auth>
            <Search>
                <Category>Shirts</Category> <SPC>S:50042,S:50316,S:50609,S:63196,S:64202,S:69820,S:69813,S:69783,S:50007,S:50066,S:50673,S:51080,S:51118,S:51625,S:67821,S:62358,S:67591,S:66971,S:63758,S:65046,S:66312,S:67336,S:67466,S:68866</SPC>
                <QuickSearch>t-shirt</QuickSearch>
                <Sort>POPULARITY</Sort>
                <StartNum>1</StartNum>
                <MaxRecs>12</MaxRecs>
                <MaxTotalItems>2500</MaxTotalItems>
                <ExtraReturnFields>ITEMNUM</ExtraReturnFields>
            </Search>
            </XMLDataStreamRequest>';
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_input);
    
    if (curl_errno($ch)) {
        echo curl_errno($ch);
        echo curl_error($ch);
    } else {
        $parsed = array();
        $response = curl_exec($ch);
        $object = simplexml_load_string($response);
    
        curl_close($ch);
        foreach ($object -> SearchResults -> Items -> Item as $key => $item) : ?>
                <a href="/products/garments/t-shirts/productview/">
                  <div id="shirt_<?php print $item->Count; ?>'" class="portfolio-box-1 three-col-por">
                    <div class="mask"></div>
                    <h3><span><?php print $item->ItemNum; ?></span><br><?php print $item->PrName; ?></h3>
                          <img src="<?php print $item->ThumbPicLink; ?>" alt="<?php print $item->PrName; ?>">
                  </div>
                      </a>
        <?php endforeach;
    }
    

    【讨论】:

    • 解决了!就像我说的,我是一个初学者,所以我很容易错过代码的“打印”部分。谢谢你的帮助——我真的很感激!
    • 我的声誉还需要 4 个,然后才给你。我点击了它,所以它应该会在我的声誉上升后立即显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2023-03-27
    • 2012-02-05
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多