_apiKey = rtrim($theData); if (strlen($this->_apiKey) < 5) { throw new Exception("Error loading API key."); } } public function URLGetTopRankedNamedEntity($url) { $this->CheckURL($url); return $this->POST("URLGetTopRankedNamedEntity", "url", "url", $url); } public function HTMLGetTopRankedNamedEntity($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetTopRankedNamedEntity", "html", "html", $html, $url); } public function TextGetTopRankedNamedEntity($text) { $this->CheckText($text); return $this->POST("TextGetTopRankedNamedEntity", "text", "text", $text); } public function URLGetRankedNamedEntities($url) { $this->CheckURL($url); return $this->POST("URLGetRankedNamedEntities", "url", "url", $url); } public function HTMLGetRankedNamedEntities($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetRankedNamedEntities", "html", "html", $html, $url); } public function TextGetRankedNamedEntities($text) { $this->CheckText($text); return $this->POST("TextGetRankedNamedEntities", "text", "text", $text); } public function URLGetNamedEntities($url) { $this->CheckURL($url); return $this->POST("URLGetNamedEntities", "url", "url", $url); } public function HTMLGetNamedEntities($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetNamedEntities", "html", "html", $html, $url); } public function TextGetNamedEntities($text) { $this->CheckText($text); return $this->POST("TextGetNamedEntities", "text", "text", $text); } public function URLGetKeywords($url) { $this->CheckURL($url); return $this->POST("URLGetKeywords", "url", "url", $url); } public function HTMLGetKeywords($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetKeywords", "html", "html", $html, $url); } public function TextGetKeywords($text) { $this->CheckText($text); return $this->POST("TextGetKeywords", "text", "text", $text); } public function URLGetLanguage($url) { $this->CheckURL($url); return $this->POST("URLGetLanguage", "url", "url", $url); } public function HTMLGetLanguage($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetLanguage", "html", "html", $html, $url); } public function TextGetLanguage($text) { $this->CheckText($text); return $this->POST("TextGetLanguage", "text", "text", $text); } public function URLGetCategory($url) { $this->CheckURL($url); return $this->POST("URLGetCategory", "url", "url", $url); } public function HTMLGetCategory($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetCategory", "html", "html", $html, $url); } public function URLGetText($url) { $this->CheckURL($url); return $this->POST("URLGetText", "url", "url", $url); } public function HTMLGetText($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetText", "html", "html", $html, $url); } public function URLGetRawText($url) { $this->CheckURL($url); return $this->POST("URLGetRawText", "url", "url", $url); } public function HTMLGetRawText($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetRawText", "html", "html", $html, $url); } public function URLGetTitle($url) { $this->CheckURL($url); return $this->POST("URLGetTitle", "url", "url", $url); } public function HTMLGetTitle($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetTitle", "html", "html", $html, $url); } public function URLGetFeedLinks($url) { $this->CheckURL($url); return $this->POST("URLGetFeedLinks", "url", "url", $url); } public function HTMLGetFeedLinks($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetFeedLinks", "html", "html", $html, $url); } public function URLGetMicroformats($url) { $this->CheckURL($url); return $this->POST("URLGetMicroformatData", "url", "url", $url); } public function HTMLGetMicroformats($html, $url) { $this->CheckHTML($html, $url); return $this->POSTHTML("HTMLGetMicroformatData", "html", "html", $html, $url); } private function CheckHTML($html, $url) { if (strlen($this->_apiKey) < 5) { throw new Exception("Load an API key."); } if (strlen($html) < 10) { throw new Exception("Enter a HTML document to analyze."); } if (strlen($url) < 10) { throw new Exception("Enter a valid URL to analyze."); } } private function CheckURL($url) { if (strlen($this->_apiKey) < 5) { throw new Exception("Load an API key."); } if (strlen($url) < 10) { throw new Exception("Enter a valid URL to analyze."); } } private function CheckText($text) { if (strlen($this->_apiKey) < 5) { throw new Exception("Load an API key."); } if (strlen($text) < 5) { throw new Exception("Enter some text to analyze."); } } private function POST($callMethod, $callPrefix, $argName, $argData) { $args = array('apikey'=> $this->_apiKey, $argName=> $argData, 'outputMode'=> 'xml', 'sourceText'=> 'raw' ); $data = ""; foreach($args as $key=>$value) { $data .= ($data != "")?"&":""; $data .= rawurlencode($key)."=".rawurlencode($value); } $params = array('http' => array('method' => 'POST', 'Content-type'=> 'application/x-www-form-urlencoded', 'Content-length' =>strlen( $data ), 'content' => $data )); $endpoint = "http://api.grid.orch8.net/calls/$callPrefix/$callMethod"; $context = stream_context_create($params); $fp = @fopen($endpoint, 'rb', false, $context); if (!($fp)) { throw new Exception("Error making API call."); } $response = @stream_get_contents($fp); fclose($fp); if ($response === false) { throw new Exception("Error making API call."); } $doc = simplexml_load_string($response); if (!($doc)) { throw new Exception("Error making API call."); } $status = $doc->xpath("/results/status"); if ($status[0] != "OK") { $statusInfo = $doc->xpath("/results/statusInfo"); throw new Exception("Error making API call: $statusInfo[0]"); } return $response; } private function POSTHTML($callMethod, $callPrefix, $argName, $argData, $url) { $args = array('apikey'=> $this->_apiKey, $argName=> $argData, 'url'=> $url, 'outputMode'=> 'xml', 'sourceText'=> 'raw' ); $data = ""; foreach($args as $key=>$value) { $data .= ($data != "")?"&":""; $data .= rawurlencode($key)."=".rawurlencode($value); } $params = array('http' => array('method' => 'POST', 'Content-type'=> 'application/x-www-form-urlencoded', 'Content-length' =>strlen( $data ), 'content' => $data )); $endpoint = "http://api.grid.orch8.net/calls/$callPrefix/$callMethod"; $context = stream_context_create($params); $fp = @fopen($endpoint, 'rb', false, $context); if (!($fp)) { throw new Exception("Error making API call."); } $response = @stream_get_contents($fp); fclose($fp); if ($response === false) { throw new Exception("Error making API call."); } $doc = simplexml_load_string($response); if (!($doc)) { throw new Exception("Error making API call."); } $status = $doc->xpath("/results/status"); if ($status[0] != "OK") { $statusInfo = $doc->xpath("/results/statusInfo"); throw new Exception("Error making API call: $statusInfo[0]"); } return $response; } } ?>