Skip to content

Instantly share code, notes, and snippets.

@dogmatic69
Created March 8, 2011 02:19
Show Gist options
  • Save dogmatic69/859732 to your computer and use it in GitHub Desktop.
Save dogmatic69/859732 to your computer and use it in GitHub Desktop.
reading feeds with a datasource in cakephp
<?php
class Gumtree extends JobAppModel{
public $name = 'Gumtree';
public $useDbConfig = 'Libs.XmlSource';
public $actsAs = false;
public $useTable = false;
public $schema = array(
'title' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 255
),
'description' => array(
'type' => 'text',
'null' => true,
'key' => 'primary',
'length' => null
),
'pubDate' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 255
),
'link' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 255
)
);
public $request = array(
'url' => 'http://www.gumtree.com/cgi-bin/list_postings.pl',
'params' => array(
'feed' => 'rss',
'search_location' => '',
'ubercat' => '',
'search_terms' => ''
)
);
public $map = array(
'data' => 'Rss.Channel.Item'
);
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
}
public function beforeFind($queryData){
if(isset($queryData['conditions'][$this->alias . '.ubercat'])){
$this->request['params']['ubercat'] = $queryData['conditions'][$this->alias . '.ubercat'];
}
if(isset($queryData['conditions'][$this->alias . '.search_terms'])){
$this->request['params']['search_terms'] = $queryData['conditions'][$this->alias . '.search_terms'];
}
$queryData['conditions'] = array();
return parent::beforeFind($queryData);
}
}
$data = $this->Gumtree->find(
'all',
array(
'conditions' => array(
'Gumtree.ubercat' => 'cars',
'Gumtree.search_terms' => 'ford'
)
)
);
// xml source gets
/*
<?xml version="1.0" encoding="UTF-8" ?>
<Rss version="2.0">
<Channel>
<title>Gumtree Stuff</title>
<description>This is an example of an RSS feed</description>
<link>http://www.someexamplerssdomain.com/main.html</link>
<lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000 </lastBuildDate>
<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
<Item>
<title>Example entry</title>
<description>Here is some text containing an interesting description.</description>
<link>http://www.wikipedia.org/</link>
<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
</Item>
</Channel>
</Rss>
'data' => 'Rss.Channel.Item' maps to <Rss><Channel><Itme></Item>
*/
// output
array(
array(
'Gumtree' => array(
'title' => 'some car',
'description' => 'foo bar...',
'pubDate' => '2009-09-06 16:45:00',
'link' => 'http://gumtree.co.uk/cars/ford-foo-bar'
)
),
array(
'Gumtree' => array(
'title' => 'some car2',
'description' => 'foo bar...',
'pubDate' => '2009-09-06 16:45:00',
'link' => 'http://gumtree.co.uk/cars/ford2-foo-bar'
)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment