为什么simplexml_load_string()返回为false?

时间:2015-08-08 17:32:18

标签: php xml api curl

更新2015-08-09:
我更新了问题标题,以更加恰当地反映我的要求 原始问题标题:
“将ebay完成api加载到simplexml_load_string()中,但我要空了”
我还设法找到了下面给出的解决方案答案。

目前我正在尝试解析来自ebays的xml数据已完成列出api。
这一点有点棘手,因为我放入浏览器的网址返回xml 但是,当我使用simplexml_load_string并使用var_dump来查看它得到的内容时:bool(false)
有人可以帮我揭开我哪里出错或者问过这个问题,请指点我
我使用了$ obj = simplexml_load_file()并收到了错误

simplexml_load_file(): I/O warning : failed to load external entity ""

我还使用了$ obj = new SimpleXMLElement()和我收到的错误:

PHP Fatal error:  Uncaught exception 'Exception' with message 'String could not be parsed as XML'

我的代码如下:

的index.php

 $appleid= appID;

function CompletedItems($search, $categoryId)
{
    global $appid;
    $url ="http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findCompletedItems&SERVICE-VERSION=1.7.0&SECURITY-APPNAME=".$appid."&GLOBAL-ID=EBAY-GB&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords=".$search."&CategoryID=".$categoryId."&itemFilter(0).name=SoldItemsOnly&itemFilter(0).value=true&sortOrder=PricePlusShippingLowest&paginationInput.entriesPerPage=1";
    $safeurl= urlencode($url);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $safeurl);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));
    $content = curl_exec($ch);
    $error   = curl_error($ch);
    curl_close($ch);

    $obj =  simplexml_load_string($content);

    echo"<h3>Completed Items</h3>";
    echo "Url: ".$url."<br>";
    var_dump($obj);

    return true;
}

我用以下函数调用该函数:CompletedItems(“iPod Touch第4代白色(8 GB)”,“73839”);

我在加载页面时得到的响应:

Url: http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findCompletedItems&SERVICE-VERSION=1.7.0&SECURITY-APPNAME=AppID&GLOBAL-ID=EBAY-GB&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords=Apple iPod Touch 4th Generation White (8 GB)&CategoryID=73839&itemFilter(0).name=SoldItemsOnly&itemFilter(0).value=true&sortOrder=PricePlusShippingLowest&paginationInput.entriesPerPage=1
bool(false)

但是,当我将链接粘贴到浏览器中时,我会收到:

<findCompletedItemsResponse>
<ack>Success</ack><version>1.13.0</version><timestamp>2015-08-08T17:25:42.574Z</timestamp>
<searchResult count="1">
<item>
<itemId>191618220158</itemId>
<title>Apple iPod Touch 4th Generation White (8 GB)   FAULTY SPARES </title>
<globalId>EBAY-GB</globalId>
<primaryCategory>
<categoryId>73839</categoryId>
<categoryName>iPods & MP3 Players</categoryName>
</primaryCategory>
<galleryURL>http://thumbs3.ebaystatic.com/m/m-18kzi7sjy70452dp8PQQw/140.jpg</galleryURL>
<viewItemURL>http://www.ebay.co.uk/itm/Apple-iPod-Touch-4th-Generation-White-8-GB-FAULTY-SPARES-/191618220158?pt=LH_DefaultDomain_3</viewItemURL>
<productId type="ReferenceID">110859287</productId>
<paymentMethod>PayPal</paymentMethod><autoPay>false</autoPay>
<location>United Kingdom</location>
<country>GB</country>
<shippingInfo>
<shippingServiceCost currencyId="GBP">1.99</shippingServiceCost>
<shippingType>Flat</shippingType>
<shipToLocations>AU</shipToLocations>
<shipToLocations>Americas</shipToLocations>
<shipToLocations>Europe</shipToLocations>
<shipToLocations>Asia</shipToLocations>
</shippingInfo><sellingStatus>
<currentPrice currencyId="GBP">3.33</currentPrice>
<convertedCurrentPrice currencyId="GBP">3.33</convertedCurrentPrice>
<bidCount>3</bidCount><sellingState>EndedWithSales</sellingState>
</sellingStatus><listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>false</buyItNowAvailable>
<startTime>2015-07-01T12:47:07.000Z</startTime>
<endTime>2015-07-08T12:47:07.000Z</endTime>
<listingType>Auction</listingType>
<gift>false</gift>
</listingInfo>
<condition>
<conditionId>7000</conditionId>
<conditionDisplayName>For parts or not working</conditionDisplayName>
</condition>
<isMultiVariationListing>false</isMultiVariationListing>
<topRatedListing>true</topRatedListing>
</item>
</searchResult>
<paginationOutput>
<pageNumber>1</pageNumber>
<entriesPerPage>1</entriesPerPage>
<totalPages>517</totalPages>
<totalEntries>517</totalEntries>
</paginationOutput>
</findCompletedItemsResponse>

1 个答案:

答案 0 :(得分:1)

我重写了我的代码,所以最终看起来像这样:

$appleid= appID;

function CompletedItems($search, $categoryId)
{
    echo"<h3>Completed Items</h3>";

    global $appid;
    $url = utf8_encode('http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findCompletedItems&SERVICE-VERSION=1.7.0&SECURITY-APPNAME='.$appid.'&GLOBAL-ID=EBAY-GB&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords='.$search.'&CategoryID=".$categoryId."&itemFilter(0).name=SoldItemsOnly&itemFilter(0).value=true&sortOrder=PricePlusShippingLowest&paginationInput.entriesPerPage=2');
    $xml = simplexml_load_file($url);
    echo $url."<br>";

    if ($xml === false) 
        {
            echo "Failed loading XML: ";
            foreach(libxml_get_errors() as $error) 
                {
                    echo "<br>", $error->message;
                }
        } 
        else 
        {
            echo"<img class='wrap' src='".$xml->searchResult->item->galleryURL."'  alt='ebay image' >";
            echo "Item Id: ".$xml->searchResult->item->itemId."<br>";
            echo "Title: ".$xml->searchResult->item->title."<br>";
            echo "Global Id: ".$xml->searchResult->item->globalId."<br>";
            echo "Primary Catergory: ".$xml->searchResult->item->primaryCategory->categoryId.", ".$xml->searchResult->item->primaryCategory->categoryName ."<br>";
            // More Code can go here
        }

    echo"<br><br><br><br>";

    return true;
}