User Tools

Site Tools


dl-xml-example

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

dl-xml-example [2009/05/08 22:47]
jay
dl-xml-example [2009/05/11 21:30] (current)
jay
Line 3: Line 3:
 ===== Overview ===== ===== Overview =====
  
-This "web service" (we're using that term pretty loosely) accepts an HTTP POST pretty much identical to the ones posted from a browser in the Iframe and Ajax approaches. However, it's assumed that this POST will be submitted by your server rather than your user's browser, and the results are returned as an XML data structure. You can then exercise your favorite XML parser and use the resulting structured data to generate formatted results in, for example PHP, a perl CGI, or Rails/Ruby.+This "web service" (we're using that term pretty loosely) accepts an HTTP POST pretty much identical to the ones posted from a browser in the Iframe and Ajax approaches. However, it's assumed that this POST will be submitted by your server rather than your user's browser, and the results are returned as an XML data structure. You can then exercise your favorite XML parser and use the resulting structured data to generate formatted results in, for example PHP, a perl CGI, Java, or Rails/Ruby.
  
 ===== Sample XML Search Results ===== ===== Sample XML Search Results =====
Line 23: Line 23:
         <phone/>         <phone/>
         <fax/>         <fax/>
 +        <label>Montreal</label>
     </location>     </location>
     <location>     <location>
Line 36: Line 37:
         <phone>(541) 338-2234</phone>         <phone>(541) 338-2234</phone>
         <fax/>         <fax/>
 +        <label>Main Office</label>
     </location>     </location>
 ...etc... ...etc...
Line 51: Line 53:
 use HTTP::Request; use HTTP::Request;
 use XML::Simple; use XML::Simple;
- +  
-my $params='&country=30';+my $params='?country=30';  # for testing, we'll look for dealers in canada 
 +my $baseurl='http://<your channelsuite URL>/API/DealerLocator/xml.cgi';
  
 my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 30); my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 30);
-my $url = "http://<your channelsuite URL>/DealerLocator/DealerLocatorWS.cgi".$params;  # a GET URL string+my $url = $baseurl.$params;  # a GET URL string
 my $header = HTTP::Request->new(GET => $url); my $header = HTTP::Request->new(GET => $url);
 my $request = HTTP::Request->new('GET', $url, $header); my $request = HTTP::Request->new('GET', $url, $header);
- +  
-# send HTTP GET request and get response in $response+# send HTTP GET request and get response object
 my $response = $agent->request($request);  my $response = $agent->request($request);
 + 
 my $data; my $data;
 if ($response->is_success) { if ($response->is_success) {
    my $xmlStream = $response->content;    my $xmlStream = $response->content;
 +   print "received XML : $xmlStream\n";
    my $xmlObject = new XML::Simple;    my $xmlObject = new XML::Simple;
-   $data = $xmlObject->XMLin($xmlStream); +   $data = $xmlObject->XMLin($xmlStream); # parse XML 
 +} else { 
 +  die $response->status_line;
 } }
-# data is a ref to a nested data structure with parsed XML data 
  
 +use Data::Dumper;  # handy perl module for dissecting your data structures
 +print Dumper $data;  # $data includes an arrayref of hashrefs with xml fields
 +
 +# now do something useful with it...
 </code> </code>
  
dl-xml-example.1241822820.txt.gz · Last modified: 2009/05/08 22:47 by jay