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:
  1. <script src='/js/scriptaculous/lib/prototype.js'></script>
  2.         <script src='/js/scriptaculous/src/scriptaculous.js'></script>
  3. <script>   
  4.     var handlerFunc = function askTagThenetForKeywords_Callback(str) {             
  5.            document.getElementById('tagthenetSuggestedTags').innerHTML = str.responseText;         
  6.       }
  7.      
  8.     function askTagThenetForKeywords()
  9.     {
  10.         document.getElementById('tagthenetSuggestedTags').innerHTML = 'Loading...';
  11.         var content = document.getElementById('txtContent').value;
  12.         new Ajax.Updater('tagthenetSuggestedTags','tagthenet-core.php?content='+content+'&maxRssItems=10', { method:'get', asynchronous:true,  onSuccess:handlerFunc });           
  13.     }
  14.    
  15.     function addTag(tagname) {
  16.     if (document.getElementById('savedTags').innerHTML == "") {
  17.         document.getElementById('savedTags').innerHTML = "<li>"+tagname+"</li>";
  18.     } else {
  19.         document.getElementById('savedTags').innerHTML +=  "<li>"+tagname+"</li>";
  20.     }
  21. }
  22. </script>
  23.  
  24. <form>
  25.     <h2>Enter some text here:</h2>
  26.     <textarea id="txtContent" name="txtContent" cols="40" rows="10"></textarea><br>
  27.     <input type="button" id="btnGetTags" value="Get Tags" onClick="askTagThenetForKeywords();">
  28. </form>       
  29.     <h2>Tags:</h2>
  30.     <div id="tagthenetSuggestedTags"></div>
  31.    
  32. <h2>Saved Tags:</h2>
  33.  
  34. <ul id="savedTags"></ul>

----------------------------------------------
tagthenet-core.php

CODE:
  1. <?php
  2.  
  3. /*
  4.     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.
  5.     Text * Example: http://tagthe.net/api/?text=Hello%20World!
  6.     URL * Example: http://tagthe.net/api/?url=http://tagthe.net
  7.     */
  8.    
  9. if(!isset($_GET['content'])){
  10.     die("No content url given");
  11. }
  12.     $content = $_GET['content'];
  13.     $keywordAPISite = "tagthe.net";
  14.     $keywordAPIUrl = "/api/?text=";
  15.     $pattern = "/(<item>)(.*?)<\/item>/i"; //.*<//tag>)/i";
  16.  
  17.     $noUnicode = preg_replace("/%u[0-9A-F]{4}/i","",$content);
  18.  
  19.     $data = urlencode(strip_tags(urldecode($noUnicode)));
  20.  
  21.     $data = str_replace('%2F','/',$data);
  22.     $data = str_replace('%09', '', $data);
  23.     $data = str_replace('%26%238217%3B','\'',$data);
  24.     $data = str_replace('%26%238220%3B','"',$data);
  25.     $data = str_replace('%26%238221%3B','"',$data);
  26.     $data = str_replace('%26%23038%3B','%26',$data);
  27.  
  28.     $curl_url = 'http://' . $keywordAPISite . $keywordAPIUrl . $data ;
  29.    
  30.     if ($debug) {
  31.             echo "Requested keywords...<br />";
  32.         }
  33.  
  34.         $xml = "";
  35.  
  36.         if ($bypost) {
  37.             $sock = fsockopen($keywordAPISite, 80, $errno, $errstr, 30);
  38.             if (!$sock) die("$errstr ($errno)\n");
  39.  
  40.             fputs($sock, "POST $keywordAPIUrl HTTP/1.0\r\n");
  41.             fputs($sock, "Host: $keywordAPISite\r\n");
  42.             fputs($sock, "Content-type: application/x-www-form-urlencoded\r\n");
  43.             fputs($sock, "Content-length: " . strlen($data) . "\r\n");
  44.             fputs($sock, "Accept: */*\r\n");
  45.             fputs($sock, "\r\n");
  46.             fputs($sock, "$data\r\n");
  47.             fputs($sock, "\r\n");
  48.  
  49.             $headers = "";
  50.             while ($str = trim(fgets($sock, 4096)))
  51.               $headers .= "$str\n";
  52.  
  53.             print "\n";
  54.  
  55.             while (!feof($sock))
  56.               $xml .= fgets($sock, 4096);
  57.  
  58.             fclose($sock);
  59.         } else if (function_exists('curl_exec')) {
  60.             $curl_conn = curl_init($curl_url);
  61.             curl_setopt( $curl_conn, CURLOPT_RETURNTRANSFER, 1 );
  62.  
  63.             $xml = curl_exec($curl_conn);
  64.         } else {
  65.             $sock = fsockopen($keywordAPISite, 80, $errno, $errstr, 30);
  66.             if (!$sock) die("$errstr ($errno)\n");
  67.  
  68.             fputs($sock, "GET " . $keywordAPIUrl . $data . " HTTP/1.0\r\n\r\n");
  69.             fputs($sock, "Host: $keywordAPISite\r\n");
  70.             fputs($sock, "Accept: */*\r\n");
  71.             $headers = "";
  72.             while ($str = trim(fgets($sock, 4096)))
  73.               $headers .= "$str\n";
  74.  
  75.             print "\n";
  76.  
  77.             while (!feof($sock))
  78.               $xml .= fgets($sock, 4096);
  79.  
  80.             fclose($sock);
  81.         }
  82.  
  83.         if ($debug) {
  84.             echo "Response is: <xmp>$xml</xmp>";
  85.             echo "Parsing response...<br />";
  86.         }
  87.  
  88.         preg_match_all($pattern, $xml, $matches);
  89.         $hasTags = false;
  90.         if ($matches) {
  91.             $hasTags = true;
  92.             foreach ($matches[2] as $match) {
  93.                     echo "<a href=\"javascript:addTag('" . str_replace(' ','_',$match) . "')\">" . $match . "</a> ";
  94.                     $tagstr .= "'" . str_replace(' ','_',$match) . "',";
  95.             }
  96.         }
  97.  
  98.         if (!$hasTags) {
  99.             echo "No tag suggestions";
  100.         }
  101.        
  102. exit;
  103. ?>

Back Top

Flickr Photo Search Using phpFlickr Building a Data Access Class using the PHP PDO Functions

Responses to “Use Script.aculo.us, PHP and tagthe.net API to Extract Tag Suggestions from text.”

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Back Top