Current location: Hot Scripts Forums » Programming Languages » PHP » reading a remote xml file into a variable


reading a remote xml file into a variable

Reply
  #1 (permalink)  
Old 12-20-05, 11:02 AM
bdee1 bdee1 is offline
New Member
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
reading a remote xml file into a variable

i am trying to write a php script which will eventually get the xml response from the amazon api and parse the xml to pull out data.

i am very new to php so i am doing this in baby steps. right now i am trying to just get the xml response from amazon and store it in a variable.

for some reason instead of an xml reponse being stored in my variable, i am just gettign all the text from the xml all squished into a single text string (no xml tags).

here is my code:
PHP Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Amazon Test</title>
</head>

<body>
<?php
    
if (! empty($_POST['searchTerms']))
    {
        print 
"Search Terms: ".$_POST['searchTerms']."<br>";
        
$requestAddress "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=myAmazonIDHere&Operation=ItemSearch&Keywords=";
        
$requestAddress .= $_POST['searchTerms'];
        
$requestAddress .= "&SearchIndex=DVD&ResponseGroup=Request,Images,SalesRank,Medium";

    
$xml_str file_get_contents($requestAddress,0);
    print 
"<br />$xml_str<br />";
}
?>

<p><strong>Search For a Movie</strong></p>
<form name="form1" method="post" action="<?php print $_SERVER['PHP_SELF']?>">
Keywords:<input type="text" name="searchTerms" size="40" />
<input type="submit" value="     Search     " />
</form>
so as you can see, first i define the request URL. then i use file_get_contents() to read the amazon response into the $xml_str variable. then i print the $xml_str variable to the screen.

i would expect it to display the following:
Code:
<?xml version="1.0" encoding="UTF-8"?>
  <ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
    <OperationRequest>
      <HTTPHeaders>
        <Header Name="UserAgent" Value="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50215; Avalon 6.0.5070; WinFX RunTime 3.0.50215)">
        </Header>
      </HTTPHeaders>
      <RequestId>0QBHYZV64PC18X8FT75N</RequestId>
      <Arguments>
        <Argument Name="SubscriptionId" Value="mySubscriptionID"></Argument>
        <Argument Name="Keywords" Value="monsters"></Argument>
        <Argument Name="ResponseGroup" Value="Request,Images,SalesRank,Medium"></Argument>
        <Argument Name="Operation" Value="ItemSearch"></Argument>
        <Argument Name="Service" Value="AWSECommerceService"></Argument>
        <Argument Name="SearchIndex" Value="DVD"></Argument>

...
but instead it displays:
Code:
1TAASZB89N1YBBJRJ0N20.275962829589844TruemonstersSalesRankRequestMediumImagesDVD13114B00005JKDRhttp://www.amazon.com/exec/obidos/redirect?tag=ws%26link_code=xm2%26camp=2025%26creative=165953%26path=http://www.amazon.com/gp/redirect.html%253fASIN=B00005JKDR%2526tag=ws%2526lcode=xm2%2526cID=2025%2526ccmID=165953%2526location=/o/ASIN/B00005JKDR%25253FSubscriptionId=1S237H5RAT7XRSK25H82313http://images.amazon.com/images/P/B00005JKDR.01._SCTHUMBZZZ_.jpg6043http://images.amazon.com/images/P/B00005JKDR.01._SCMZZZZZZZ_.jpg14099http://images.amazon.com/images/P/B00005JKDR.01._SCLZZZZZZZ_.jpg475335http://images.amazon.com/images/P/B00005JKDR.01._SCTHUMBZZZ_.jpg6043http://images.amazon.com/images/P/B00005JKDR.01._SCMZZZZZZZ_.jpg14099http://images.amazon.com/images/P/B00005JKDR.01._SCLZZZZZZZ_.jpg4753351.85:1G (General Audience)Peter DocterDavid SilvermanLee Unkrich0786936164886AnimatedClosed-captionedColorDolbyDubbedWidescreenNTSCSpanishDubbedEnglishOriginal Language2999USD$29.992DVD12002-09-1793Disney/Pixar2001-11-027869361648861749USD$17.491050USD$10.501300USD$13.00664780Amazon.comThe folks at Pixar can do no wrong with <I>Monsters, Inc.</I>, the studio's fourth feature film, which stretches the computer animation format in terms of both technical complexity and emotional impact. The giant, blue-furred James P. "Sulley" Sullivan (wonderfully voiced by John Goodman) is a scare-monster extraordinaire in the hidden world of Monstropolis, where the scaring of kids is an imperative in order to keep the entire city running.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-20-05, 01:32 PM
bdee1 bdee1 is offline
New Member
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Ok so i figured that maybe thats just the way the xml displays if you try and do a standard print so i thought i woudl try to go ahead and parse it. so here is the page with the new xml parsing code:
PHP Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Amazon Test</title>
</head>

<body>
<?php
    
if (! empty($_POST['searchTerms']))
    {
        print 
"Search Terms: ".$_POST['searchTerms']."<br>";
        
$requestAddress "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=1S237H5RAT7XRSK25H82&Operation=ItemSearch&Keywords=";
        
$requestAddress .= $_POST['searchTerms'];
        
$requestAddress .= "&SearchIndex=DVD&ResponseGroup=Request,Images,SalesRank,Medium";
        
        
        
$parser xml_parser_create();
        
        
xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
        
xml_set_element_handler$parser"start_handler""end_handler" );
        
xml_set_character_data_handler($parser"char_data");
        
        
$xml_str file_get_contents($requestAddress,0);
        
//print "<br />$xml_str<br />";
        
        
xml_parse($parser$xml_str) or die( format_error($parser));
        
        function 
start_handler($parser$el_name$attribs)
        {
            print 
"START: $el_name: <br />\n";
            foreach(
$attribs as $at_name=>$at_val)
            {
                print 
"\t$at_name=>\"$at_val\"<br />\n";
            }
            print 
"\t<blockquote><div>\n";
        }
        
        function 
end_handler($parser$el_name)
        {
            print 
"\t</div></blockquote>\n";
            print 
"END: $el_name<br />\n";
        }
        
        function 
char_data($parser$data)
        {
            print 
"\tchar data:<i>".trim($data)."</i><br />\n";
        }
        
        function 
format_error ($p)
        {
            
$code xml_get_error_code($p);
            
$str xml_error_string($code);
            
$line xml_get_current_line_number ($p);
            return 
"XML ERROR ($code): $str at line $line";
            
        }
        
    }
?>



<p><strong>Search For a Movie</strong></p>
<form name="form1" method="post" action="<?php print $_SERVER['PHP_SELF']?>">
Keywords:<input type="text" name="searchTerms" size="40" />
<input type="submit" value="     Search     " />
</form>

</body>
</html>
when i run it, and enter a search phrase, i get the following errors:
Code:
Warning: xml_parse() [function.xml-parse]: Unable to call handler start_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler start_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler start_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler start_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler end_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler end_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler start_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler char_data() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler end_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler start_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler start_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler end_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler start_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28

Warning: xml_parse() [function.xml-parse]: Unable to call handler end_handler() in C:\Inetpub\SVIntranet\PHPTest\myAmazon.php on line 28
and they go on and on.

i looked around on google and most of the time it sounds like this occurs because the handler functions were not created but as you can see i have the handler functions.

can someone please give me some insight as to whats going on here?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form to Text File Justin171985 PHP 14 02-09-11 12:55 PM
templating problem (how to forward file name as a variable) skyrat PHP 0 09-11-05 09:27 PM
reading text from a file waji ASP 2 11-10-04 12:41 AM
Perl Script to parse 1 xml file into many flsh Perl 0 08-09-04 11:21 AM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 05:28 PM


All times are GMT -5. The time now is 09:11 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.