Jump to content

google base xml parse


beermaker74

Recommended Posts

ok I am just trying to query google base and parse the result.

 

here is the result from one item from google

<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gm='http://base.google.com/ns-metadata/1.0' xmlns:g='http://base.google.com/ns/1.0' xmlns:batch='http://schemas.google.com/gdata/batch'>
<id>http://www.google.com/base/feeds/snippets</id>
<updated>2007-02-26T20:33:03.539Z</updated>
<title type='text'>Items matching query: (rental [location:@"San Francisco,CA"+50mi]) [item type:housing]</title>
<link rel='alternate' type='text/html' href='http://base.google.com'></link>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.google.com/base/feeds/snippets'></link>
<link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='http://www.google.com/base/feeds/snippets/batch'></link>
<link rel='self' type='application/atom+xml' href='http://www.google.com/base/feeds/snippets/-/housing?start-index=1&max-results=1&bq=rental+%5Blocation%3A%40%22San+Francisco%2CCA%22%2B50mi%5D'></link>
<link rel='next' type='application/atom+xml' href='http://www.google.com/base/feeds/snippets/-/housing?start-index=2&max-results=1&bq=rental+%5Blocation%3A%40%22San+Francisco%2CCA%22%2B50mi%5D'></link>
<author>
<name>Google Inc.</name>
<email>[email protected]</email>
</author>
<generator version='1.0' uri='http://base.google.com'>GoogleBase</generator>
<openSearch:totalResults>5812</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>1</openSearch:itemsPerPage>
<entry>
<id>http://www.google.com/base/feeds/snippets/2978077361409212904</id>
<published>2007-02-26T12:10:23.000Z</published>
<updated>2007-02-26T12:12:20.000Z</updated>
<category scheme='http://base.google.com/categories/itemtypes' term='Housing'></category>
<title type='text'>3 bedroom apartment rental in Berkeley California</title>
<content type='html'>Elegant 3BR victorian unit available for short term rental 1/2 block to Bart transportation and close to the Berkeley Bowl (a great gourmet food market) and close to UC Berkeley.Unit was featured in Architectural Digest and has black walls wtih  ...</content>
<link rel='alternate' type='text/html' href='http://www.sublet.com/spider/supplydetails_adv.asp?SupplyId=337581&source=10228'></link>
<link rel='self' type='application/atom+xml' href='http://www.google.com/base/feeds/snippets/2978077361409212904'></link>
<author>
<name>[email protected]</name>
<email>[email protected]</email>
</author>
<g:image_link type='url'>http://base.google.com/base_image?q=http%3A%2F%2Fwww.sublet.com%2Faptimages%2F337581_1.jpg&dhm=53a74726&size=6</g:image_link>
<g:city type='text'>Oakland</g:city><g:image_link type='url'>http://base.google.com/base_image?q=http%3A%2F%2Fwww.sublet.com%2Faptimages%2F337581_4.jpg&dhm=57e7adc0&size=6</g:image_link>
<g:label type='text'>apartment for rent</g:label>
<g:customer_id type='int'>1072962</g:customer_id>
<g:bathrooms type='float'>1.0</g:bathrooms>
<g:label type='text'>Oakland</g:label>
<g:expiration_date type='dateTime'>2007-03-12T12:10:23.000Z</g:expiration_date>
<g:label type='text'>vacation rental</g:label>
<g:label type='text'>short term rental</g:label><g:item_type type='text'>Housing</g:item_type>
<g:property_type type='text'>apartment</g:property_type>
<g:label type='text'>temporary housing</g:label>
<g:price type='floatUnit'>2500.0 usd</g:price>
<g:label type='text'>California</g:label>
<g:area type='text'>Berkeley</g:area>
<g:item_language type='text'>EN</g:item_language>
<g:image_link type='url'>http://base.google.com/base_image?q=http%3A%2F%2Fwww.sublet.com%2Faptimages%2F337581_3.jpg&dhm=5b620579&size=6</g:image_link>
<g:location type='location'>California, 94703, USA</g:location>
<g:label type='text'>vacation home</g:label>
<g:label type='text'>holiday rental</g:label>
<g:label type='text'>Berkeley</g:label>
<g:area type='float'>1500.0</g:area>
<g:bedrooms type='int'>3</g:bedrooms>
<g:agent type='text'>Rinna</g:agent>
<g:image_link type='url'>http://base.google.com/base_image?q=http%3A%2F%2Fwww.sublet.com%2Faptimages%2F337581_2.jpg&dhm=ffffffffac1031de&size=6</g:image_link>
<g:listing_type type='text'>vacation rental</g:listing_type>
<g:target_country type='text'>US</g:target_country>
<g:id type='text'>337581</g:id>
<g:image_link type='url'>http://base.google.com/base_image?q=http%3A%2F%2Fwww.sublet.com%2Faptimages%2F337581_5.jpg&dhm=ffffffffee4c399a&size=6</g:image_link>
</entry>
</feed>

 

here is my code that I hacked from yahoo to parse it

<?php
$base = 'http://google.com/base/feeds/snippets/-/';
$params = 'rental+%5blocation:@%22San+Francisco,CA%22%2b50mi%5d&max-results=1';

$url = $base . 'housing?bq=' . $params;

$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($c);
curl_close($c);

$dom = domxml_open_mem($response, DOMXML_LOAD_PARSING, $errors);

function xml_to_result($dom) {
   $root = $dom->document_element();
   $res['totalResultsAvailable'] = $root->get_attribute('totalResultsAvailable');
   $res['totalResultsReturned'] = $root->get_attribute('totalResultsReturned');
   $res['firstResultPosition'] = $root->get_attribute('firstResultPosition');

   $node = $root->first_child();
   $i = 0;
   while($node) {
      switch($node->tagname) {
      case 'entry':
         $subnode = $node->first_child();
         while($subnode) {
            $subnodes = $subnode->child_nodes();
               if(!empty($subnodes)) {
                  foreach($subnodes as $k=>$n) {
                     if(empty($n->tagname)) {
                        $res[$i][$subnode->tagname] = trim($n->get_content());
                     } else {
                        $res[$i][$subnode->tagname][$n->tagname]=trim($n->get_content());
                     }
                  }
               }
            $subnode = $subnode->next_sibling();
         }
         break;
      default:
         $res[$node->tagname] = trim($node->get_content());
         $i--;
         break;
      }
      $i++;
      $node = $node->next_sibling();
   }
return $res;
}

$res = xml_to_result($dom);

echo '<pre>';
print_r($res);
echo '</pre>';
?>

 

here is the result from that code print_r

Array
(
    [totalResultsAvailable] => 
    [totalResultsReturned] => 
    [firstResultPosition] => 
    [id] => http://www.google.com/base/feeds/snippets
    [updated] => 2007-02-26T20:42:47.146Z
    [title] => Items matching query: (rental [location:@"San Francisco,CA"+50mi]) [item type:housing]
    [link] => 
    [author] => Google [email protected]
    [generator] => GoogleBase
    [totalResults] => 5805
    [startIndex] => 1
    [itemsPerPage] => 1
    [0] => Array
        (
            [id] => 337581
            [published] => 2007-02-26T12:10:23.000Z
            [updated] => 2007-02-26T12:12:20.000Z
            [title] => 3 bedroom apartment rental in Berkeley California
            [content] => Elegant 3BR victorian unit available for short term rental 1/2 block to Bart transportation and close to the Berkeley Bowl (a great gourmet food market) and close to UC Berkeley.Unit was featured in Architectural Digest and has black walls wtih  ...
            [author] => Array
                (
                    [name] => [email protected]
                    [email] => [email protected]
                )

            [city] => Oakland
            [item_language] => EN
            [image_link] => http://base.google.com/base_image?q=http%3A%2F%2Fwww.sublet.com%2Faptimages%2F337581_5.jpg&dhm=ffffffffee4c399a&size=6
            [label] => apartment for rent
            [target_country] => US
            [agent] => Rinna
            [price] => 2500.0 usd
            [bathrooms] => 1.0
            [area] => Berkeley
            [listing_type] => vacation rental
            [property_type] => apartment
            [expiration_date] => 2007-03-12T12:10:23.000Z
            [item_type] => Housing
            [bedrooms] => 3
            [location] => California, 94703, USA
            [customer_id] => 1072962
        )

)

the links are not coming through. they are blank. It seems that most other elements are there. The feed is an atom feed and the links seem to be encoded differently. I would really like to have access to the links. I am sure it is an easy fix. I am still a bit confused at all this xml parsing stuff. By the way I don't have php5 on my server so i cant use simplexml.

thanks

Link to comment
https://forums.phpfreaks.com/topic/40246-google-base-xml-parse/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.