【问题标题】:Why simplexml_load_string() returing as false?为什么 simplexml_load_string() 返回为假?
【发布时间】:2015-08-08 17:32:17
【问题描述】:

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


目前我正在尝试从 ebays Completed Listing api 解析 xml 数据。
目前有点棘手,因为我放入浏览器的 url 返回 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 4th Generation White (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>

【问题讨论】:

标签: php xml api curl


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 2014-02-03
    • 2012-08-27
    • 2014-09-07
    • 2013-04-18
    • 2013-08-26
    • 2013-10-17
    • 1970-01-01
    • 2019-01-07
    相关资源
    最近更新 更多