October 27th, 2006
Use Script.aculo.us, PHP and tagthe.net API to Extract Tag Suggestions from text.
AJAX, PHP, by jay.This is something I was working on a few weeks ago.
What this demonstrates is the ability to send a block of text to a web services and have that service responds with a list of tags. Just to spice this up a bit I add in some Ajax.
index.php
CODE:
-
<script src='/js/scriptaculous/lib/prototype.js'></script>
-
<script src='/js/scriptaculous/src/scriptaculous.js'></script>
-
<script>
-
var handlerFunc = function askTagThenetForKeywords_Callback(str) {
-
document.getElementById('tagthenetSuggestedTags').innerHTML = str.responseText;
-
}
-
-
function askTagThenetForKeywords()
-
{
-
document.getElementById('tagthenetSuggestedTags').innerHTML = 'Loading...';
-
var content = document.getElementById('txtContent').value;
-
new Ajax.Updater('tagthenetSuggestedTags','tagthenet-core.php?content='+content+'&maxRssItems=10', { method:'get', asynchronous:true, onSuccess:handlerFunc });
-
}
-
-
function addTag(tagname) {
-
if (document.getElementById('savedTags').innerHTML == "") {
-
document.getElementById('savedTags').innerHTML = "<li>"+tagname+"</li>";
-
} else {
-
document.getElementById('savedTags').innerHTML += "<li>"+tagname+"</li>";
-
}
-
}
-
</script>
-
-
<form>
-
<h2>Enter some text here:</h2>
-
<textarea id="txtContent" name="txtContent" cols="40" rows="10"></textarea><br>
-
<input type="button" id="btnGetTags" value="Get Tags" onClick="askTagThenetForKeywords();">
-
</form>
-
<h2>Tags:</h2>
-
<div id="tagthenetSuggestedTags"></div>
-
-
<h2>Saved Tags:</h2>
-
-
<ul id="savedTags"></ul>
----------------------------------------------
tagthenet-core.php
CODE:
-
<?php
-
-
/*
-
Send a HTTP GET or POST request to http://tagthe.net/api/?[METHOD]=[VALUE]. You will receive a response document with the tags we found in the text.
-
Text * Example: http://tagthe.net/api/?text=Hello%20World!
-
URL * Example: http://tagthe.net/api/?url=http://tagthe.net
-
*/
-
-
if(!isset($_GET['content'])){
-
die("No content url given");
-
}
-
$content = $_GET['content'];
-
$keywordAPISite = "tagthe.net";
-
$keywordAPIUrl = "/api/?text=";
-
$pattern = "/(<item>)(.*?)<\/item>/i"; //.*<//tag>)/i";
-
-
$noUnicode = preg_replace("/%u[0-9A-F]{4}/i","",$content);
-
-
$data = urlencode(strip_tags(urldecode($noUnicode)));
-
-
$data = str_replace('%2F','/',$data);
-
$data = str_replace('%09', '', $data);
-
$data = str_replace('%26%238217%3B','\'',$data);
-
$data = str_replace('%26%238220%3B','"',$data);
-
$data = str_replace('%26%238221%3B','"',$data);
-
$data = str_replace('%26%23038%3B','%26',$data);
-
-
$curl_url = 'http://' . $keywordAPISite . $keywordAPIUrl . $data ;
-
-
if ($debug) {
-
echo "Requested keywords...<br />";
-
}
-
-
$xml = "";
-
-
if ($bypost) {
-
$sock = fsockopen($keywordAPISite, 80, $errno, $errstr, 30);
-
if (!$sock) die("$errstr ($errno)\n");
-
-
fputs($sock, "POST $keywordAPIUrl HTTP/1.0\r\n");
-
fputs($sock, "Host: $keywordAPISite\r\n");
-
fputs($sock, "Content-type: application/x-www-form-urlencoded\r\n");
-
fputs($sock, "Content-length: " . strlen($data) . "\r\n");
-
fputs($sock, "Accept: */*\r\n");
-
fputs($sock, "\r\n");
-
fputs($sock, "$data\r\n");
-
fputs($sock, "\r\n");
-
-
$headers = "";
-
while ($str = trim(fgets($sock, 4096)))
-
$headers .= "$str\n";
-
-
print "\n";
-
-
while (!feof($sock))
-
$xml .= fgets($sock, 4096);
-
-
fclose($sock);
-
} else if (function_exists('curl_exec')) {
-
$curl_conn = curl_init($curl_url);
-
curl_setopt( $curl_conn, CURLOPT_RETURNTRANSFER, 1 );
-
-
$xml = curl_exec($curl_conn);
-
} else {
-
$sock = fsockopen($keywordAPISite, 80, $errno, $errstr, 30);
-
if (!$sock) die("$errstr ($errno)\n");
-
-
fputs($sock, "GET " . $keywordAPIUrl . $data . " HTTP/1.0\r\n\r\n");
-
fputs($sock, "Host: $keywordAPISite\r\n");
-
fputs($sock, "Accept: */*\r\n");
-
$headers = "";
-
while ($str = trim(fgets($sock, 4096)))
-
$headers .= "$str\n";
-
-
print "\n";
-
-
while (!feof($sock))
-
$xml .= fgets($sock, 4096);
-
-
fclose($sock);
-
}
-
-
if ($debug) {
-
echo "Response is: <xmp>$xml</xmp>";
-
echo "Parsing response...<br />";
-
}
-
-
preg_match_all($pattern, $xml, $matches);
-
$hasTags = false;
-
if ($matches) {
-
$hasTags = true;
-
foreach ($matches[2] as $match) {
-
echo "<a href=\"javascript:addTag('" . str_replace(' ','_',$match) . "')\">" . $match . "</a> ";
-
$tagstr .= "'" . str_replace(' ','_',$match) . "',";
-
}
-
}
-
-
if (!$hasTags) {
-
echo "No tag suggestions";
-
}
-
-
exit;
-
?>
I got this:
404 Not Found Not Found The requested URL /api/ was not found on this server. Apache/2.2.3 (Debian) mod_jk/1.2.18 Server at memanage.knallgrau.at Port 80