A YQL example in javascript and in PHP
Following on from my quick attempt at using YQL to trawl through Senate and House of Reps debates on the Australian internet filter being proposed I thought I’d stick the code up here as some people might find it useful to see how you can use YQL in just about any language to get the results you want.
First off I made a very quick javascript version which just grabs the list and appends each piece of returned data to the page.
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.0");
</script>
</head>
<body>
<h1>A list of debates in the Australian Senate including the words internet+filter</h1>
<div id="container">
</div>
</body>
<script type="text/javascript">
function parseDebates(data) {
var results = data.query.results.match;
$.each(results, function() {
var $div = $('<div />');
if(this.speaker) {
var name = $('<p>')
.append(this.speaker.first_name+" "+this.speaker.last_name)
.append(' on '+this.hdate+' in '+this.parent.body+'');
var content = $('<blockquote>'+this.body+'</blockquote>');
}
$div.append(name);
$div.append(content)
$('#container').append($div);
});
}
</script>
<script type="text/javascript" src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20openaustralia.getDebates%20where%20type%3D'senate'%20and%20search%3D'internet%20filter'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=parseDebates"></script>
And then we can do essentially exactly the same thing in PHP, except instead of appending the data to the page we take the same returned results, generate an RSS xml schema and print that.
<?php
$apicall = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20openaustralia.getDebates%20where%20type%3D'senate'%20and%20search%3D'internet%20filter'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apicall);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$debates = json_decode($output);
$output = '<?xml version="1.0"?>';
$output .= '<rss version="2.0">';
$output .= '<channel>';
$output .= '<title>Internet Filter debates in the senate</title>';
$output .= '<description>A list of debates mentioning the words Internet+Filter in the Australian Senate</description>';
$output .= '<link>http://www.hallofbrightcarvings.com/external/internet-filter.html</link>';
foreach($debates->query->results->match as $mention) {
if($mention->speaker) {
$output .= '<item>';
$output .= '<title><![CDATA['.$mention->speaker->first_name.' '.$mention->speaker->last_name.' on '.$mention->hdate.' in '.$mention->parent->body.']]></title>';
$output .= '<description><![CDATA['.$mention->body.']]></description>';
$output .= '<link><![CDATA[http://www.openaustralia.org'.$mention->listurl.']]></link>';
$output .= '</item>';
}
}
$output .= '</channel>';
$output .= '</rss>';
header("Content-type: text/xml");
echo $output;
Category: Web Development Comment »