0

I have this php function in xampp server using php 5.5, using a live server with php 5.3 I get no error. The error says

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\wordpresstest\wp-content\plugins\Monetizer_Pro_Monetizer\Monetizer.php:910
Stack trace:
#0 C:\xampp\htdocs\wordpresstest\wp-content\plugins\Monetizer_Pro_Monetizer\Monetizer.php(910): SimpleXMLElement->__construct('')
#1 [internal function]: Monetizer->Commission_Junction(Array, '', 'Commission Junc...')
#2 C:\xampp\htdocs\wordpresstest\wp-includes\shortcodes.php(273): call_user_func(Array, Array, '', 'Commission Junc...')
#3 [internal function]: do_shortcode_tag(Array)
#4 C:\xampp\htdocs\wordpresstest\wp-includes\shortcodes.php(188): preg_replace_callback('/[([?)(embed|...', 'do_shortcode_ta...', '[Amazon keyword...')
#5 [internal function]: do_shortcode('[Amazon keyword...')
#6 C:\xampp\htdocs\wordpresstest\wp-includes\plugin.php(199): call_user_func_array('do_shortcode', Array)
#7 C:\xampp\htdocs\wordpresstest\wp-includes\post-template.php(166): apply_filters('the_content', '[Amazon keyword...')
#8 C:\xampp in C:\xampp\htdocs\wordpresstest\wp-content\plugins\Monetizer_Pro_Monetizer\Monetizer.php on line 910

The function is

public function Commission_Junction($atts) {
            extract( shortcode_atts( array(
                'keyword' => '',
                'results' => '',
                'context' => '',
            ), $atts ) );

            $params = shortcode_atts(array(
                'keyword' => '',
                'results' => '',
                'context' => '',
            ), $atts);
            if ($params['context'] !== 'yes') {
                $linkcloakeroptions = get_option('LinkCloaker_Settings');
                $linkcloakerpath = $linkcloakeroptions['Main_Path'];
                if (file_exists($_SERVER['DOCUMENT_ROOT'].$linkcloakerpath.'/Monetizer_Pro_LinkCloaker/LinkCloaker.php')) {
                    $LinkCloaker = new LinkCloaker();
                    $LinkCloakerexist = true;
                    $arv = $LinkCloaker->getFirstAvailablePage();
                    $linkpage = $arv[0];
                    $folder = $arv[1]; 
                    $linkpage = $LinkCloaker->createPage($linkpage, $folder);
                }
            }
            $contextuallinkoptions = get_option('ContextualLink_Settings');
            $contextuallinkpath = $contextuallinkoptions['Main_Path'];
            if (file_exists($_SERVER['DOCUMENT_ROOT'].$contextuallinkpath.'/Monetizer_Pro_ContextualLink/ContextualLink.php')) {
                $ContextualLink = new ContextualLink();
            }
            if ($params['context'] == "yes") {
                return $ContextualLink->start("Commission_Junction", $params['results']);
            }
            else {
                $options = get_option('Monetizer_Settings');
                $monetizerpath = $options['Main_Path'];
                $cloak = $options['Commission_Junction_Link_Check'];
                $cDevKey = $options['Commission_Junction_API_Key'];
                $nWebSiteID = $options['Commission_Junction_Site_ID'];
                $cURL = 'https://product-search.api.cj.com/v2/product-search?';
                $cURL .= 'website-id=' . $nWebSiteID;
                $cURL .= '&keywords=' . $params['keyword'];
                $cURL .= '&records-per-page=' . $params['results'];
                $cURL .= '&page-number=' . '1';
                $cURL .= '&advertiser-ids=' . $options['Commission_Junction_Advertiser_Relationship'] . '&';
                $cURL .= '&serviceable-area=' . $options['Commission_Junction_Country'];
                if ($options['Commission_Junction_Low_Price'] != '') {
                    $cURL .= '&low-price=' . $options['Commission_Junction_Low_Price'];
                }
                if ($options['Commission_junction_High_price'] != '') {
                   $cURL .= '&high-price=' . $options['Commission_junction_High_price']; 
                }

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $cURL);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                              'Authorization: ' . $cDevKey,
                              'User-Agent: "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 GTB6 (.NET CLR 3.5.30729)"'
                            ));

                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);

                $response = curl_exec($ch);
                curl_close($ch); 

                //$xml = simplexml_load_string($response);
                $xml = new SimpleXMLElement($response);
                if ($LinkCloakerexist) {
                    $linknumber = 0;
                }
                foreach ($xml->products->product as $item) {
                    echo $item;
                }
                if ($xml) {
                    if (isset($options['Commission_Junction_Products_Display_Heading'])) {
                        $product .= "<h3>".$options['Commission_Junction_Products_Display_Heading']."</h3><br />";
                    }
                    $template = $options['Commission_Junction_Post_Template'];
                   foreach ($xml->products->product as $item) {
                        if ($options['User_Level'] == 'basic') {
                            $this_row = '<div style="display:table-row-group;width:'.$options['Commission_Junction_Post_Width'].'px;"><a href=%link%><img width="150px" height="150px" src=%imgURL%></a><p style="float:right;width:'.($options['Commission_Junction_Post_Width']-200).'px;text-align:left;"><a href=%link% class="Commission_Junction_Post_A">%title%<br /><span class="Commission_Junction_Post_Font">%price%</span> from %merchantname%</a><br /><span class="Commission_Junction_Post_Font">%description%</span></p></div>';
                        }
                        else if ($options['User_Level'] == 'advanced') {
                            $this_row = $template;
                        }

                        $link  = $item->xpath('buy-url');
                        $link  = (string)$link[0];
                        if ($LinkCloakerexist && $cloak == true) {
                            $LinkCloaker = new LinkCloaker();
                            $arv = $LinkCloaker->createCloakedLink($link, $linknumber, $linkpage, $folder, false, $options['Commission_Junction_Auto_Generated']);  // link complete with your aff ID

                            $link = $arv[0];
                            $linknumber = $arv[1];
                            $linknumber++;
                        }
                        else {
                            $link = $link;
                        }

                        $title = $item->xpath('name');
                        $title = (string)$title[0];

                        $imgURL = $item->xpath('image-url');
                        $imgURL = (string)$imgURL[0];

                        if ($imgURL == '') {
                            $imgURL = $_SERVER['DOCUMENT_ROOT'].$monetizerpath.'/Monetizer_Pro_Monetizer/images/noimage.png';
                        }
                        else {
                            $imgURL = $imgURL;
                        }

                        $price = $item->xpath('price');
                        $price = '$'.number_format((float)$price[0],2,'.',',');

                        $merchantname = $item->xpath('advertiser-name');
                        $merchantname = (string)$merchantname[0];

                        $description = $item->xpath('description');
                        $description = (string)$description[0];

                        $adid = $item->xpath('ad-id');
                        $adid = (string)$adid[0];

                        $advertiserid = $item->xpath('advertiser-id');
                        $advertiserid = (string)$advertiserid[0];

                        $advertisername = $item->xpath('advertiser-name');
                        $advertisername = (string)$advertisername[0];

                        $advertisercategory = $item->xpath('advertiser-category');
                        $advertisercategory = (string)$advertisercategory[0];

                        $catalogid = $item->xpath('catalog-id');
                        $catalogid = (string)$catalogid[0];

                        $currency = $item->xpath('currency');
                        $currency = (string)$currency[0];

                        $instock = $item->xpath('in-stock');
                        $instock = (string)$instock[0];

                        $isbn = $item->xpath('isbn');
                        $isbn = (string)$isbn[0];

                        $manufacturername = $item->xpath('manufacturer-name');
                        $manufacturername = (string)$manufacturername[0];

                        $manufacturersku = $item->xpath('manufacturer-sku');
                        $manufacturersku = (string)$manufacturersku[0];

                        $retailprice = $item->xpath('retail-price');
                        $retailprice = (string)$retailprice[0];

                        $saleprice = $item->xpath('sale-price');
                        $saleprice = (string)$saleprice[0];

                        $sku = $item->xpath('sku');
                        $sku = (string)$sku[0];

                        $upc = $item->xpath('upc');
                        $upc = (string)$upc[0];
                        $replace_array = array(
                                '%link%' => $link,
                                '%title%' => $title,
                                '%imgURL%' => $imgURL,
                                '%price%' => $price,
                                '%merchantname%' => $merchantname,
                                '%description%' => $this->DescriptionLength($description),
                                '%adid%' => $adid,
                                '%advertiserid%' => $advertiserid,
                                '%advertisername%' => $advertisername,
                                '%advertisercategory%' => $advertisercategory,
                                '%catalogid%' => $catalogid,
                                '%currency%' => $currency,
                                '%instock%' => $instock,
                                '%isbn%' => $isbn,
                                '%manufacturername%' => $manufacturername,
                                '%manufacturersku%' => $manufacturersku,
                                '%retailprice%' => $retailprice,
                                '%saleprice%' => $saleprice,
                                '%sku%' => $sku,
                                '%upc%' => $upc,
                            );
                            foreach($replace_array as $search => $replace) {
                                $this_row = str_replace($search, $replace, $this_row);
                            }

                        if($link != "") {
                            $product .= $this_row;
                        }
                   }

                    if ($product == '') { 
                    }
                    if ($cloak == true) {
                        $LinkCloaker->createCloakedLink($link, $linknumber, $linkpage, $folder, true, $options['Commission_Junction_Auto_Generated']);
                    }
                    return $product;
                }
            }
        }

Why do I receive this error? And what do I do to fix it. Thanks.

2
  • 2
    Ask the author of the Monetizer_Pro_Monetizer plugin, it's their code. Commented Jan 31, 2014 at 21:08
  • 2
    i am the author Commented Jan 31, 2014 at 21:12

2 Answers 2

0

Andrew:

As already mentioned, your curl call seems to be returning FALSE. It also appears than when you try to create a SimpleXML element with a boolean FALSE that it throws the exception you are seeing. I have also had problems accessing APIs via curl when on my localhost. I was able to solve it by adding this code:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2);

Also try adding this, although for my script it was not necessary:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

2
  • Fixing an uncaught Exception with a message of 'String could not be parsed as XML' tells me that it has nothing to do with cURL. Anyway, please file an edit and explain why you think this would help.
    – kaiser
    Commented Jan 31, 2014 at 22:50
  • that solved the problem and loads the products properly, did not have to add curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ) thanks Commented Feb 5, 2014 at 2:51
2

I tried to run your code. The first trivial guess is that the curl operation

$response = curl_exec($ch);

return false. May are many reasons for this! timeout, wrong call, wrong authkey, ... and this is why the

$xml = new SimpleXMLElement($response);

returns an error! Simly check the value of $response before try to xml parse it.

2
  • thank you i can test it, i think i may know what is causing the problem, after a few months i have to regenerate the key and i think this is what has happened, beacuse it is not showing products on the live server Commented Jan 31, 2014 at 22:09
  • ok the key was not the problem, and the live server using all the same info shows products and no error Commented Jan 31, 2014 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.