Repository navigation
Expand file tree
/
Copy pathtagservices.php
More file actions
92 lines (87 loc) · 2.57 KB
/
Copy pathtagservices.php
File metadata and controls
92 lines (87 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/* Requires a phrase to tag (?text=) and an engineID (&engine=) and a timer flag (&timer=)
* Returns as set of tags in the format [type1: value1, value2, .. valuen], ...[typen: ,, ..]
* Engine IDs are:
* 0 (default) Tagthe.net
* 1 Yahoo
* 2 Alchemy
*
* To Do: 1. Add more engines
* 2. Manage Error handling - drop into another engine when call fails
*
*
*/
function tagservices($text, $engine){
//if (isset($_REQUEST['_SESSION'])) die("Muppet!"); //handle session injection attack
//$timer = (isset($_GET['timer']));
//$text = $_GET['text'];
//$engine = $_GET['engine'];
$tags="";
$timer=false;
$msc=microtime(true);
switch ($engine){
case 1: //Yahoo YQL
$tags = '[';
$text = str_replace('"',"'", $text); //yahoo objects to quotes
$q = urlencode("select * from search.termextract where context =\"$text\"");
$raw=getremotefile("http://query.yahooapis.com/v1/public/yql?q=$q");
if ($raw != false){
if (strpos($raw, "<!---------------->")>0){
$tags='YQL ERROR';
break;
}
libxml_use_internal_errors(true);
$tagxml=simplexml_load_string($raw);
if (!$tagxml) {
$myFile = "tagxmlerror.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
foreach(libxml_get_errors() as $error) {
fwrite( $fh, $error->message);
}
fwrite($fh, $raw);
fclose($fh);
}
$results = $tagxml->results[0];
foreach ($results->Result as $result){
$tags .= $result . ", ";
}
if (strlen($tags) > 2) {$tags = substr($tags, 0, -2). "]";}
break;
}
case 2: //Alchemy
$key="28f7145e6d69472fb7faf22ae45bc429a9ac52e5";
$raw= getremotefile("http://access.alchemyapi.com/calls/text/TextGetRankedKeywords?apikey=$key&text=".urlencode($text));
if ($raw != false){
$tagxml=simplexml_load_string($raw);
$results = $tagxml->keywords[0];
foreach ($results->keyword as $result){
$tags .= $result->text . ", ";
}
if (strlen($tags) > 2) {$tags = substr($tags, 0, -2). "]";}
break;
}
case 3: //awaiting code
break;
default:
$tags= "[";
$raw= getremotefile("http://tagthe.net/api/?text=".urlencode($text));
if ($raw != false){
$tagxml=simplexml_load_string($raw);
$meme = $tagxml->meme[0];
foreach($meme->dim as $dim){
if ($dim['type'] != 'language'){
$tags .= "[" . $dim['type'].": ";
foreach ($dim->item as $item){
$tags .= $item . ", ";
}
if (strlen($tags) > 2) {$tags = substr($tags, 0, -2). "]";}
}
}
} else {$tags = "[tagthenet unavailable]";}
}
if ($timer){
$tags.= "[". intval((microtime(true)-$msc) * 1000). " msecs]";
}
return $tags;
}
?>