doufeixuan8882 2015-08-08 17:32
浏览 248
已采纳

为什么simplexml_load_string()返回false?

update 2015-08-09:
I updated the question title to reflect more accuretly what I was asking
Original question title:
"loading ebay completed api into simplexml_load_string() but i am coming up empty"
I have also managed to find a solution answer given below.


At the moment I am trying to parse xml data from ebays Completed listing api.
It's a little tricky at the moment because the url I put into a browser returns xml.
However when I Use simplexml_load_string and have var_dump to see what it has I get: bool(false)
Can someone help me uncover where I went wrong or if this question has already been asked, please point me there

I have used $obj = simplexml_load_file () and recieved an error

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

I have also used $obj = new SimpleXMLElement() and the error i recived:

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

My code is as follows:

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

I call the function with: CompletedItems("iPod Touch 4th Generation White (8 GB)", "73839");

the responce I get when I Load the page:

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)

However when I paste the link into a browser I receive this:

<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条回答 默认 最新

  • dongxuanyi3406 2015-08-09 12:40
    关注

    I rewrote my code so ultimatly it looks like this:

    $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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器