PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/core/utilities/calais/opencalais.php

http://github.com/newscloud/open-social-media-toolkit
PHP | 129 lines | 92 code | 28 blank | 9 comment | 8 complexity | e41c66a7599661303e151064af6ec6ef MD5 | raw file
  1. <?php
  2. /**
  3. * Open Calais Tags
  4. * Last updated 7/6/2009
  5. * Copyright (c) 2009 Dan Grossman
  6. * http://www.dangrossman.info
  7. *
  8. * Please see http://www.dangrossman.info/open-calais-tags
  9. * for documentation and license information.
  10. */
  11. class OpenCalaisException extends Exception {}
  12. class OpenCalais {
  13. const APIURL = "http://api1.opencalais.com/enlighten/rest/";
  14. private $apikey;
  15. private $allowDistribution = false;
  16. private $allowSearch = false;
  17. private $externalID = '';
  18. private $submitter = 'Open Calais Tags';
  19. private $contentType = 'text/txt';
  20. private $outputFormat = 'xml/rdf';
  21. private $prettyTypes = true;
  22. private $entities;
  23. public function OpenCalais($apikey = null) {
  24. if (empty($apikey)) {
  25. throw new OpenCalaisException("You must provide an OpenCalais API key to use this class.");
  26. } else {
  27. $this->apikey = $apikey;
  28. }
  29. $this->entities = array();
  30. }
  31. public function getAllowDistribution() { return $this->allowDistribution; }
  32. public function getAllowSearch() { return $this->allowSearch; }
  33. public function getExternalID() { return $this->externalID; }
  34. public function getSubmitter() { return $this->submitter; }
  35. public function getContentType() { return $this->contentType; }
  36. public function getOutputFormat() { return $this->outputFormat; }
  37. public function getPrettyTypes() { return $this->prettyTypes; }
  38. public function setAllowDistribution($x) { $this->allowDistribution = $x; }
  39. public function setAllowSearch($x) { $this->allowSearch = $x; }
  40. public function setExternalID($x) { $this->externalID = $x; }
  41. public function setSubmitter($x) { $this->submitter = $x; }
  42. public function setContentType($x) { $this->contentType = $x; }
  43. public function setOutputFormat($x) { $this->outputFormat = $x; }
  44. public function setPrettyTypes($x) { $this->prettyTypes = $x; }
  45. public function getEntities($content) {
  46. $response = $this->callAPI($content);
  47. $xml = substr($response, strpos($response, 'c:document'));
  48. $pattern = '#' . preg_quote('<c:name>', '#') . '(.*?)' . preg_quote('</c:name>', '#') . '#ms';
  49. $matches = preg_match_all($pattern, $xml, $rdf, PREG_SET_ORDER);
  50. foreach ($rdf as $arr) {
  51. $this->addEntity($arr[1]);
  52. }
  53. return $this->entities;
  54. }
  55. private function addEntity($val) {
  56. if (!in_array($val, $this->entities))
  57. $this->entities[] = $val;
  58. }
  59. private function callAPI($content, $title = null) {
  60. $postdata['licenseID'] = $this->apikey;
  61. $postdata['paramsXML'] =
  62. '<c:params xmlns:c="http://s.opencalais.com/1/pred/"'
  63. . ' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'
  64. . ' <c:processingDirectives c:contentType="' . $this->contentType
  65. . '" c:outputFormat="' . $this->outputFormat . '" c:enableMetadataType="SocialTags"></c:processingDirectives>'
  66. . ' <c:userDirectives c:allowDistribution="' . $this->allowDistribution
  67. . '" c:allowSearch="' . $this->allowSearch . '" c:externalID="' . $this->externalID
  68. . '" c:submitter="' . $this->submitter . '"></c:userDirectives>'
  69. . ' <c:externalMetadata></c:externalMetadata>'
  70. . '</c:params>';
  71. if (!empty($content)) {
  72. $postdata['content'] = $content;
  73. } else {
  74. throw new OpenCalaisException("Content to analyze is empty.");
  75. }
  76. $poststring = $this->urlencodeArray($postdata);
  77. $ch = curl_init();
  78. curl_setopt($ch, CURLOPT_URL, self::APIURL);
  79. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  80. curl_setopt($ch, CURLOPT_HEADER, 0);
  81. curl_setopt($ch, CURLOPT_POSTFIELDS, $poststring);
  82. curl_setopt($ch, CURLOPT_POST, 1);
  83. $response = html_entity_decode(curl_exec($ch));
  84. if (strpos($response, "<Exception>") !== false) {
  85. $text = preg_match("/\<Exception\>(.*)\<\/Exception\>/mu", $response, $matches);
  86. throw new OpenCalaisException($matches[1]);
  87. }
  88. return $response;
  89. }
  90. private function urlencodeArray($array) {
  91. foreach ($array as $key => $val) {
  92. if (!isset($string)) {
  93. $string = $key . "=" . urlencode($val);
  94. } else {
  95. $string .= "&" . $key . "=" . urlencode($val);
  96. }
  97. }
  98. return $string;
  99. }
  100. }
  101. ?>