PrismaPixel Studios
PrismaPixel Studios

Reputation: 247

Trouble with parsing XML data from cURL POST in PHP

I am working on integrating a catalog functionality to our company website that I have designed, akserigraphics.com. I can get the response from the cURL POST but I can't seem to get it to format properly to create the catalog view on the .php page.

Here is the layout that I am attempting to have the XML data populate in PHP. Below is the code that I am using and below that is the output I am receiving.

CODE

<?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;        
    }
?>

OUTPUT

Array ( [ "1.0" encoding="UTF-8"?>
3.2 USE SUBJECT TO TERMS OF YOUR AGREEMENT. UNAUTHORIZED USE PROHIBITED. SUPPLIER INFORMATION IS CONFIDENTIAL. (C) 2018 QUICK TECHNOLOGIES INC. 1 1000 1 343212964 HVKQF-HAUXO
Port [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-Shirt PC55 5_62_-_7_62 http://www_promoplace_com/ws/ws_dll/QPic?SN] =>
50042 [amp;_Company®_Essential_T-Shirt PC61 5_96_-_7_96 http://www_promoplace_com/ws/ws_dll/QPic?SN] => 50042 )

I have been searching high and low on the internet for several days and can't seem to identify what I am doing wrong. I am only a beginner when it comes to PHP so any help would be greatly appreciated.

Upvotes: 1

Views: 389

Answers (1)

user9189147
user9189147

Reputation: 296

Hope this helps. You also may want to remove your API credentials from this request? If this API is restricted just for your usage? I have removed them from my answer below and replaced with ####. Also, I am using PHP's simplexml_load_string to get the response into an object for iteration. I have tested this locally and it worked. Let me know if you have any questions?

<?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;
}

Upvotes: 1

Related Questions