SimpleXML Help & Checking file exists help

Discussion in 'PHP' started by crazyryan, Jun 22, 2008.

  1. #1
    Hey

    I have two things I need to sort out, simplexml and checking if a file exists.

    Firstly, simpleXML.

    I know how to parse all the data but I always get confused on how to access it...

    This is the feed I'm pulling data from, I want to access <item> but I'm not sure what path thing I do, e.g
    
    $amazonKey = "1QXMAKM6KD613QF2XT02";
    $buyAmazon = simplexml_load_file("http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=" . $amazonKey . "&Operation=ItemSearch&Keywords=" . $track->title . "&SearchIndex=Music&ResponseGroup=Medium&Version=2008-04-07");
    
    echo $buyAmazon->[B]ItemSearchResponse->Items->Item[0]->[/B]DetailPageUrl;
    
    Code (markup):
    That shows nothing so I assume the bit I bolded is wrong, and I dont know what else to put..

    Secondly, whats the best way to check if a remote file is a valid MP3 file?
     
    crazyryan, Jun 22, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    print_r($buyAmazon);

    You can see exactly what route you would need to access the data.

    Dan
     
    Danltn, Jun 22, 2008 IP
  3. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #3
    Thanks for the help - I've done that and got
    SimpleXMLElement Object ( [OperationRequest] => SimpleXMLElement Object ( [HTTPHeaders] => SimpleXMLElement Object ( [Header] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => UserAgent ) ) ) [RequestId] => 1WSA8ZPKPVK8Z8J0AQNQ [Arguments] => SimpleXMLElement Object ( [Argument] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => SearchIndex [Value] => Music ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Service [Value] => AWSECommerceService ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Keywords [Value] => Lollipop (Remix) feat. Gorilla Zoe ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => SubscriptionId [Value] => 1QXMAKM6KD613QF2XT02 ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => ResponseGroup [Value] => Medium ) ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Operation [Value] => ItemSearch ) ) [6] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Version [Value] => 2008-04-07 ) ) ) ) [RequestProcessingTime] => 0.0317389965057373 ) [Items] => SimpleXMLElement Object ( [Request] => SimpleXMLElement Object ( [IsValid] => True [ItemSearchRequest] => SimpleXMLElement Object ( [Keywords] => Lollipop (Remix) feat. Gorilla Zoe [ResponseGroup] => Medium [SearchIndex] => Music ) [Errors] => SimpleXMLElement Object ( [Error] => SimpleXMLElement Object ( [Code] => AWS.ECommerceService.NoExactMatches [Message] => We did not find any matches for your request. ) ) ) [TotalResults] => 0 [TotalPages] => 0 ) ) [/code]
    
    PHP:
    Which doesnt do that much apart from confuse me.
     
    crazyryan, Jun 22, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Try viewing the source or using:

    echo '<pre>';
    print_r($buyAmazon);

    Very obvious then.
     
    Danltn, Jun 22, 2008 IP
  5. J.T.D.

    J.T.D. Peon

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you want to print every item, use this code:

    
    $amazonKey = "1QXMAKM6KD613QF2XT02"; //your key
    $buyAmazon = simplexml_load_file("http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=" . $amazonKey . "&Operation=ItemSearch&Keywords=" . $track->title . "&SearchIndex=Music&ResponseGroup=Medium&Version=2008-04-07"); //the file
    
    for ( $i=1; $i < $buyAmazon->ItemSearchResponse->TotalResults; $i++ ) { //starts a counter
       foreach ($buyAmazon->ItemSearchResponse->Items as $item) { //for every item
          echo 'URL: <a href="'.$item.'">Result #'.$i.'</a>'; //echo the url
       }
    }
    
    PHP:
     
    J.T.D., Jun 22, 2008 IP
  6. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #6
    Thanks, but I'm still confused, for example, I have the following in my code:

    
    if(!empty($getSuggestions->resultList->result[0]->artist)) {
    echo '<p>Perhaps you were looking for a song by one of the following artists?</p>';
    
    Code (markup):
    Since I only ever have one result, I just want to see if the record is actually there, so I need to check if DetailPageURL is empty or not, like the code I just posted.
     
    crazyryan, Jun 23, 2008 IP
  7. J.T.D.

    J.T.D. Peon

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    
    $amazonKey = "1QXMAKM6KD613QF2XT02"; //your key
    $buyAmazon = simplexml_load_file("http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=" . $amazonKey . "&Operation=ItemSearch&Keywords=" . $track->title . "&SearchIndex=Music&ResponseGroup=Medium&Version=2008-04-07"); //the file
    
    $detail = $buyAmazon->ItemSearchResponse->Items->DetailPageURL; //<!-- the url -- note: it's just the first item
    
    if ( empty($detail) ) {
      //UH-OH!!! It's empty!!
    } else {
      //It's not empty
    }
    
    PHP:
     
    J.T.D., Jun 23, 2008 IP