PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/profile/neurohub_commons/modules/contrib/tagging/modules/tagging_alchemy_suggest/AlchemyAPI.php

https://bitbucket.org/drn05r/neurohub-dev-davidn
PHP | 555 lines | 413 code | 142 blank | 0 comment | 46 complexity | 208d471788ce698f1f09e38f15c1feb4 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. require_once("AlchemyAPIParams.php");
  3. class AlchemyAPI
  4. {
  5. const XML_OUTPUT_MODE = "xml";
  6. const JSON_OUTPUT_MODE = "json";
  7. private $_apiKey = '';
  8. private $_hostPrefix = 'access';
  9. public function setAPIHost($apiHost)
  10. {
  11. $this->_hostPrefix = $apiHost;
  12. if (strlen($this->_hostPrefix) < 2)
  13. {
  14. throw new Exception("Error setting API host.");
  15. }
  16. }
  17. public function setAPIKey($apiKey)
  18. {
  19. $this->_apiKey = $apiKey;
  20. if (strlen($this->_apiKey) < 5)
  21. {
  22. throw new Exception("Error setting API key.");
  23. }
  24. }
  25. public function loadAPIKey($filename)
  26. {
  27. $handle = fopen($filename, 'r');
  28. $theData = fgets($handle, 512);
  29. fclose($handle);
  30. $this->_apiKey = rtrim($theData);
  31. if (strlen($this->_apiKey) < 5)
  32. {
  33. throw new Exception("Error loading API key.");
  34. }
  35. }
  36. public function URLGetRankedNamedEntities($url, $outputMode = self::XML_OUTPUT_MODE, $namedEntityParams = null)
  37. {
  38. $this->CheckURL($url, $outputMode);
  39. $this->CheckParamType("AlchemyAPI_NamedEntityParams", $namedEntityParams);
  40. if(is_null($namedEntityParams))
  41. $namedEntityParams = new AlchemyAPI_NamedEntityParams();
  42. $namedEntityParams->setUrl($url);
  43. $namedEntityParams->setOutputMode($outputMode);
  44. return $this->GET("URLGetRankedNamedEntities", "url", $namedEntityParams);
  45. }
  46. public function HTMLGetRankedNamedEntities($html, $url, $outputMode = self::XML_OUTPUT_MODE, $namedEntityParams = null)
  47. {
  48. $this->CheckHTML($html, $url, $outputMode);
  49. $this->CheckParamType("AlchemyAPI_NamedEntityParams", $namedEntityParams);
  50. if(is_null($namedEntityParams))
  51. $namedEntityParams = new AlchemyAPI_NamedEntityParams();
  52. $namedEntityParams->setHtml($html);
  53. $namedEntityParams->setUrl($url);
  54. $namedEntityParams->setOutputMode($outputMode);
  55. return $this->POST("HTMLGetRankedNamedEntities", "html", $namedEntityParams);
  56. }
  57. public function TextGetRankedNamedEntities($text, $outputMode = self::XML_OUTPUT_MODE, $namedEntityParams = null)
  58. {
  59. $this->CheckText($text, $outputMode);
  60. $this->CheckParamType("AlchemyAPI_NamedEntityParams", $namedEntityParams);
  61. if(is_null($namedEntityParams))
  62. $namedEntityParams = new AlchemyAPI_NamedEntityParams();
  63. $namedEntityParams->setText($text);
  64. $namedEntityParams->setOutputMode($outputMode);
  65. return $this->POST("TextGetRankedNamedEntities", "text", $namedEntityParams);
  66. }
  67. public function URLGetRankedKeywords($url, $outputMode = self::XML_OUTPUT_MODE, $keywordParams = null)
  68. {
  69. $this->CheckURL($url, $outputMode);
  70. $this->CheckParamType("AlchemyAPI_KeywordParams", $keywordParams);
  71. if(is_null($keywordParams))
  72. $keywordParams = new AlchemyAPI_KeywordParams();
  73. $keywordParams->setUrl($url);
  74. $keywordParams->setOutputMode($outputMode);
  75. return $this->GET("URLGetRankedKeywords", "url", $keywordParams);
  76. }
  77. public function HTMLGetRankedKeywords($html, $url, $outputMode = self::XML_OUTPUT_MODE, $keywordParams = null)
  78. {
  79. $this->CheckHTML($html, $url, $outputMode);
  80. $this->CheckParamType("AlchemyAPI_KeywordParams", $keywordParams);
  81. if(is_null($keywordParams))
  82. $keywordParams = new AlchemyAPI_KeywordParams();
  83. $keywordParams->setHtml($html);
  84. $keywordParams->setUrl($url);
  85. $keywordParams->setOutputMode($outputMode);
  86. return $this->POST("HTMLGetRankedKeywords", "html", $keywordParams);
  87. }
  88. public function TextGetRankedKeywords($text, $outputMode = self::XML_OUTPUT_MODE, $keywordParams = null)
  89. {
  90. $this->CheckText($text, $outputMode);
  91. $this->CheckParamType("AlchemyAPI_KeywordParams", $keywordParams);
  92. if(is_null($keywordParams))
  93. $keywordParams = new AlchemyAPI_KeywordParams();
  94. $keywordParams->setText($text);
  95. $keywordParams->setOutputMode($outputMode);
  96. return $this->POST("TextGetRankedKeywords", "text", $keywordParams);
  97. }
  98. public function URLGetLanguage($url, $outputMode = self::XML_OUTPUT_MODE, $languageParams = null)
  99. {
  100. $this->CheckURL($url, $outputMode);
  101. $this->CheckParamType("AlchemyAPI_LanguageParams", $languageParams);
  102. if(is_null($languageParams))
  103. $languageParams = new AlchemyAPI_LanguageParams();
  104. $languageParams->setUrl($url);
  105. $languageParams->setOutputMode($outputMode);
  106. return $this->GET("URLGetLanguage", "url", $languageParams);
  107. }
  108. public function HTMLGetLanguage($html, $url, $outputMode = self::XML_OUTPUT_MODE, $languageParams = null)
  109. {
  110. $this->CheckHTML($html, $url, $outputMode);
  111. $this->CheckParamType("AlchemyAPI_LanguageParams", $languageParams);
  112. if(is_null($languageParams))
  113. $languageParams = new AlchemyAPI_LanguageParams();
  114. $languageParams->setHtml($html);
  115. $languageParams->setUrl($url);
  116. $languageParams->setOutputMode($outputMode);
  117. return $this->POST("HTMLGetLanguage", "html", $languageParams);
  118. }
  119. public function TextGetLanguage($text, $outputMode = self::XML_OUTPUT_MODE, $languageParams = null)
  120. {
  121. $this->CheckText($text, $outputMode);
  122. $this->CheckParamType("AlchemyAPI_LanguageParams", $languageParams);
  123. if(is_null($languageParams))
  124. $languageParams = new AlchemyAPI_LanguageParams();
  125. $languageParams->setText($text);
  126. $languageParams->setOutputMode($outputMode);
  127. return $this->POST("TextGetLanguage", "text", $languageParams);
  128. }
  129. public function URLGetCategory($url, $outputMode = self::XML_OUTPUT_MODE, $categorizeParams = null)
  130. {
  131. $this->CheckURL($url, $outputMode);
  132. $this->CheckParamType("AlchemyAPI_CategoryParams", $categorizeParams);
  133. if(is_null($categorizeParams))
  134. $categorizeParams = new AlchemyAPI_CategoryParams();
  135. $categorizeParams->setUrl($url);
  136. $categorizeParams->setOutputMode($outputMode);
  137. return $this->GET("URLGetCategory", "url", $categorizeParams);
  138. }
  139. public function HTMLGetCategory($html, $url, $outputMode = self::XML_OUTPUT_MODE, $categorizeParams = null)
  140. {
  141. $this->CheckHTML($html, $url, $outputMode);
  142. $this->CheckParamType("AlchemyAPI_CategoryParams", $categorizeParams);
  143. if(is_null($categorizeParams))
  144. $categorizeParams = new AlchemyAPI_CategoryParams();
  145. $categorizeParams->setHtml($html);
  146. $categorizeParams->setUrl($url);
  147. $categorizeParams->setOutputMode($outputMode);
  148. return $this->POST("HTMLGetCategory", "html", $categorizeParams);
  149. }
  150. public function TextGetCategory($text, $outputMode = self::XML_OUTPUT_MODE, $categorizeParams = null)
  151. {
  152. $this->CheckText($text, $outputMode);
  153. $this->CheckParamType("AlchemyAPI_CategoryParams", $categorizeParams);
  154. if(is_null($categorizeParams))
  155. $categorizeParams = new AlchemyAPI_CategoryParams();
  156. $categorizeParams->setText($text);
  157. $categorizeParams->setOutputMode($outputMode);
  158. return $this->POST("TextGetCategory", "text", $categorizeParams);
  159. }
  160. public function URLGetText($url, $outputMode = self::XML_OUTPUT_MODE, $textParams = null)
  161. {
  162. $this->CheckURL($url, $outputMode);
  163. $this->CheckParamType("AlchemyAPI_TextParams", $textParams);
  164. if(is_null($textParams))
  165. $textParams = new AlchemyAPI_TextParams();
  166. $textParams->setUrl($url);
  167. $textParams->setOutputMode($outputMode);
  168. return $this->GET("URLGetText", "url", $textParams);
  169. }
  170. public function HTMLGetText($html, $url, $outputMode = self::XML_OUTPUT_MODE, $textParams = null)
  171. {
  172. $this->CheckHTML($html, $url, $outputMode);
  173. $this->CheckParamType("AlchemyAPI_TextParams", $textParams);
  174. if(is_null($textParams))
  175. $textParams = new AlchemyAPI_TextParams();
  176. $textParams->setHtml($html);
  177. $textParams->setUrl($url);
  178. $textParams->setOutputMode($outputMode);
  179. return $this->POST("HTMLGetText", "html", $textParams);
  180. }
  181. public function URLGetRawText($url, $outputMode = self::XML_OUTPUT_MODE, $textParams = null)
  182. {
  183. $this->CheckURL($url, $outputMode);
  184. $this->CheckParamType("AlchemyAPI_TextParams", $textParams);
  185. if(is_null($textParams))
  186. $textParams = new AlchemyAPI_TextParams();
  187. $textParams->setUrl($url);
  188. $textParams->setOutputMode($outputMode);
  189. return $this->GET("URLGetRawText", "url", $textParams);
  190. }
  191. public function HTMLGetRawText($html, $url, $outputMode = self::XML_OUTPUT_MODE, $textParams = null)
  192. {
  193. $this->CheckHTML($html, $url, $outputMode);
  194. $this->CheckParamType("AlchemyAPI_TextParams", $textParams);
  195. if(is_null($textParams))
  196. $textParams = new AlchemyAPI_TextParams();
  197. $textParams->setHtml($html);
  198. $textParams->setUrl($url);
  199. $textParams->setOutputMode($outputMode);
  200. return $this->POST("HTMLGetRawText", "html", $textParams);
  201. }
  202. public function URLGetTitle($url, $outputMode = self::XML_OUTPUT_MODE, $textParams = null)
  203. {
  204. $this->CheckURL($url, $outputMode);
  205. $this->CheckParamType("AlchemyAPI_TextParams", $textParams);
  206. if(is_null($textParams))
  207. $textParams = new AlchemyAPI_TextParams();
  208. $textParams->setUrl($url);
  209. $textParams->setOutputMode($outputMode);
  210. return $this->GET("URLGetTitle", "url", $textParams);
  211. }
  212. public function HTMLGetTitle($html, $url, $outputMode = self::XML_OUTPUT_MODE, $textParams = null)
  213. {
  214. $this->CheckHTML($html, $url, $outputMode);
  215. $this->CheckParamType("AlchemyAPI_TextParams", $textParams);
  216. if(is_null($textParams))
  217. $textParams = new AlchemyAPI_TextParams();
  218. $textParams->setHtml($html);
  219. $textParams->setUrl($url);
  220. $textParams->setOutputMode($outputMode);
  221. return $this->POST("HTMLGetTitle", "html", $textParams);
  222. }
  223. public function URLGetFeedLinks($url, $outputMode = self::XML_OUTPUT_MODE, $params = null)
  224. {
  225. $this->CheckURL($url, $outputMode);
  226. $this->CheckParamType("AlchemyAPI_Params", $params);
  227. if(is_null($params))
  228. $params = new AlchemyAPI_Params();
  229. $params->setUrl($url);
  230. $params->setOutputMode($outputMode);
  231. return $this->GET("URLGetFeedLinks", "url", $params);
  232. }
  233. public function HTMLGetFeedLinks($html, $url, $outputMode = self::XML_OUTPUT_MODE, $params = null)
  234. {
  235. $this->CheckHTML($html, $url, $outputMode);
  236. $this->CheckParamType("AlchemyAPI_Params", $params);
  237. if(is_null($params))
  238. $params = new AlchemyAPI_Params();
  239. $params->setHtml($html);
  240. $params->setUrl($url);
  241. $params->setOutputMode($outputMode);
  242. return $this->POST("HTMLGetFeedLinks", "html", $params);
  243. }
  244. public function URLGetMicroformats($url, $outputMode = self::XML_OUTPUT_MODE, $params = null)
  245. {
  246. $this->CheckURL($url, $outputMode);
  247. $this->CheckParamType("AlchemyAPI_Params", $params);
  248. if(is_null($params))
  249. $params = new AlchemyAPI_Params();
  250. $params->setUrl($url);
  251. $params->setOutputMode($outputMode);
  252. return $this->GET("URLGetMicroformatData", "url", $params);
  253. }
  254. public function HTMLGetMicroformats($html, $url, $outputMode = self::XML_OUTPUT_MODE, $params = null)
  255. {
  256. $this->CheckHTML($html, $url, $outputMode);
  257. $this->CheckParamType("AlchemyAPI_Params", $params);
  258. if(is_null($params))
  259. $params = new AlchemyAPI_Params();
  260. $params->setHtml($html);
  261. $params->setUrl($url);
  262. $params->setOutputMode($outputMode);
  263. return $this->POST("HTMLGetMicroformatData", "html", $params);
  264. }
  265. public function URLGetConstraintQuery($url, $query, $outputMode = self::XML_OUTPUT_MODE, $constraintParams = null)
  266. {
  267. $this->CheckURL($url, $outputMode);
  268. $this->CheckParamType("AlchemyAPI_ConstraingQueryParams", $constraintParams);
  269. if (strlen($query) < 2)
  270. {
  271. throw new Exception("Invalid constraint query specified.");
  272. }
  273. if(is_null($constraintParams))
  274. $constraintParams = new AlchemyAPI_ConstraintQueryParams();
  275. $constraintParams->setUrl($url);
  276. $constraintParams->setOutputMode($outputMode);
  277. $constraintParams->setCQuery($query);
  278. return $this->GET("URLGetConstraintQuery", "url", $constraintParams);
  279. }
  280. public function HTMLGetConstraintQuery($html, $url, $query, $outputMode = self::XML_OUTPUT_MODE, $constraintParams = null)
  281. {
  282. $this->CheckHTML($html, $url, $outputMode);
  283. $this->CheckParamType("AlchemyAPI_ConstraingQueryParams", $constraintParams);
  284. if (strlen($query) < 2)
  285. {
  286. throw new Exception("Invalid constraint query specified.");
  287. }
  288. $constraintParams = new AlchemyAPI_ConstraintQueryParams();
  289. $constraintParams->setUrl($url);
  290. $constraintParams->setHtml($html);
  291. $constraintParams->setOutputMode($outputMode);
  292. $constraintParams->setCQuery($query);
  293. return $this->POST("HTMLGetConstraintQuery", "html", $constraintParams);
  294. }
  295. private function CheckOutputMode($outputMode)
  296. {
  297. if (strlen($this->_apiKey) < 5)
  298. {
  299. throw new Exception("Load an API key.");
  300. }
  301. if (self::XML_OUTPUT_MODE !== $outputMode &&
  302. self::JSON_OUTPUT_MODE !== $outputMode)
  303. {
  304. throw new Exception("Illegal Output Mode specified, see *_OUTPUT_MODE constants.");
  305. }
  306. }
  307. private function CheckURL($url, $outputMode)
  308. {
  309. $this->CheckOutputMode($outputMode);
  310. if (strlen($url) < 10)
  311. {
  312. throw new Exception("Enter a valid URL to analyze.");
  313. }
  314. }
  315. private function CheckHTML($html, $url, $outputMode)
  316. {
  317. $this->CheckURL($url, $outputMode);
  318. if (strlen($html) < 10)
  319. {
  320. throw new Exception("Enter a HTML document to analyze.");
  321. }
  322. }
  323. private function CheckText($text, $outputMode)
  324. {
  325. $this->CheckOutputMode($outputMode);
  326. if (strlen($text) < 5)
  327. {
  328. throw new Exception("Enter some text to analyze.");
  329. }
  330. }
  331. private function CheckParamType($className, $class)
  332. {
  333. if(!is_null($class) && ($className != get_class($class)) )
  334. {
  335. throw new Exception("Trying to pass ".get_class($class)." into a function that requires ".$className);
  336. }
  337. }
  338. private function POST()
  339. { // callMethod, $callPrefix, $parameterObject
  340. $callMethod = func_get_arg(0);
  341. $callPrefix = func_get_arg(1);
  342. $paramObj = func_get_arg(2);
  343. $outputMode = $paramObj->getOutputMode();
  344. $data = "apikey=".$this->_apiKey."&outputMode=".$outputMode.$paramObj->getParameterString();
  345. $params = array('http' => array('method' => 'POST',
  346. 'Content-type'=> 'application/x-www-form-urlencoded',
  347. 'Content-length' =>strlen( $data ),
  348. 'content' => $data
  349. ));
  350. $hostPrefix = $this->_hostPrefix;
  351. $endpoint = "http://$hostPrefix.alchemyapi.com/calls/$callPrefix/$callMethod";
  352. $context = stream_context_create($params);
  353. return $this->DoRequest($endpoint,$context,$outputMode);
  354. }
  355. private function GET()
  356. { // callMethod, $callPrefix, $parameterObject
  357. $callMethod = func_get_arg(0);
  358. $callPrefix = func_get_arg(1);
  359. $paramObj = func_get_arg(2);
  360. $outputMode = $paramObj->getOutputMode();
  361. $data = "apikey=".$this->_apiKey."&outputMode=".$outputMode.$paramObj->getParameterString();
  362. $params = array('http' => array('method' => 'GET',
  363. 'Content-type'=> 'application/x-www-form-urlencoded'
  364. ));
  365. $hostPrefix = $this->_hostPrefix;
  366. $uri = "http://$hostPrefix.alchemyapi.com/calls/$callPrefix/$callMethod"."?".$data;
  367. $context = stream_context_create($params);
  368. return $this->DoRequest($uri,$context,$outputMode);
  369. }
  370. private function DoRequest($uri,$context, $outputMode)
  371. {
  372. $fp = @fopen($uri, 'rb', false, $context);
  373. if (!($fp))
  374. {
  375. throw new Exception("Error making API call.");
  376. }
  377. $response = @stream_get_contents($fp);
  378. fclose($fp);
  379. if ($response === false)
  380. {
  381. throw new Exception("Error making API call.");
  382. }
  383. if (self::XML_OUTPUT_MODE == $outputMode)
  384. {
  385. $doc = simplexml_load_string($response);
  386. if (!($doc))
  387. {
  388. throw new Exception("Error making API call.");
  389. }
  390. $status = $doc->xpath("/results/status");
  391. if ($status[0] != "OK")
  392. {
  393. $statusInfo = $doc->xpath("/results/statusInfo");
  394. throw new Exception("Error making API call: $statusInfo[0]");
  395. }
  396. }
  397. else
  398. {
  399. $obj = json_decode($response);
  400. if (is_null($obj))
  401. {
  402. throw new Exception("Error making API call.");
  403. }
  404. if ("OK" != $obj->{'status'})
  405. {
  406. $statusInfo = $obj->{'statusInfo'};
  407. throw new Exception("Error making API call: $statusInfo");
  408. }
  409. }
  410. return $response;
  411. }
  412. }
  413. ?>