在WordPress中使用Xampp接收错误异常

时间:2014-01-31 作者:disertation

我在使用php 5.5的xampp服务器中有这个php函数,在使用带有php 5.3的实时服务器时,我没有收到任何错误。错误显示

致命错误:在C:\\xampp\\htdocs\\wordpresstest\\wp content\\plugins\\Monetizer\\u Pro\\u Monetizer\\Monetizer中,未捕获异常“exception”,消息“String无法解析为XML”。php:910堆栈跟踪:0 C:\\xampp\\htdocs\\wordpresstest\\wp content\\plugins\\Monetizer\\u Pro\\u Monetizer\\Monetizer。php(910):SimpleXMLElement->\\u构造(“”)
\\1[内部函数]:货币化工具->佣金连接(数组,,“佣金连接…”)
\\2 C:\\xampp\\htdocs\\wordpresstest\\wp包括\\短代码。php(273):call\\u user\\u func(Array,Array,,,\'Commission Junc…\')
\\3[内部函数]:do\\u shortcode\\u tag(Array)
\\4 C:\\xampp\\htdocs\\wordpresstest\\wp includes\\shortcode。php(188):preg\\u replace\\u回调(\'/[([?)(embed |…\',\'do\\u shortcode\\u ta…\',\'Amazon keyword…\')5[内部函数]:do\\u shortcode(\'Amazon keyword…\')6 C:\\xampp\\htdocs\\wordpresstest\\wp includes\\plugin。php(199):call\\u user\\u func\\u array(\'do\\u shortcode\',array)
\\7 C:\\xampp\\htdocs\\wordpresstest\\wp includes\\post template。php(166):apply\\u filters(\'the\\u content\',”“[亚马逊关键词…”)
#8 C:\\xampp in C:\\xampp\\htdocs\\wordpresstest\\wp content\\plugins\\Monetizer\\u Pro\\u Monetizer\\Monetizer。php在线910

功能是

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;
                }
            }
        }
为什么我会收到此错误?我该怎么做才能修复它。谢谢

2 个回复
最合适的回答,由SO网友:nawrockp 整理而成

安德鲁:

如前所述,curl调用似乎返回FALSE。当您尝试创建一个带有布尔值FALSE的SimpleXML元素时,它也会抛出您看到的异常。在本地主机上通过curl访问api时,我也遇到了问题。我可以通过添加以下代码来解决此问题:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2);
也可以尝试添加此内容,但对于我的脚本来说,这不是必需的:

curl\\u setopt($ch,CURLOPT\\u IPRESOLVE,curl\\u IPRESOLVE\\u V4);

SO网友:stefano

我试着运行你的代码。第一个简单的猜测是curl操作

$response = curl_exec($ch);
返回false。可能有很多原因!超时、错误呼叫、错误身份验证密钥。。。这就是为什么

$xml = new SimpleXMLElement($response);
返回错误!在尝试xml解析之前,请简单检查$response的值。

结束