Exemple #1
0
 public static function processRequest()
 {
     // get our verb
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $response = new RestResponse();
     // we'll store our data here
     $data = array();
     switch ($request_method) {
         // gets are easy...
         case 'get':
             $data = $_GET;
             break;
             // so are posts
         // so are posts
         case 'post':
             $data = $_POST;
             break;
             // here's the tricky bit...
         // here's the tricky bit...
         case 'put':
             // basically, we read a string from PHP's special input location,
             // and then parse it out into an array via parse_str... per the PHP docs:
             // Parses str  as if it were the query string passed via a URL and sets
             // variables in the current scope.
             parse_str(file_get_contents('php://input'), $put_vars);
             $data = $put_vars;
             break;
         default:
             // TODO. Hvis vi når hertil bør vi nok råbe av.
             break;
     }
     if (isset($_GET['q'])) {
         $data['q'] = $_GET['q'];
     }
     // store the method
     $response->setMethod($request_method);
     // set the raw data, so we can access it if needed (there may be
     // other pieces to your requests)
     $response->setRequestVars($data);
     if (isset($data['data'])) {
         // TODO
         // translate the JSON to an Object for use however you want
         $response->setData(json_decode($data['data']));
     }
     $path = $response->getRequestVars();
     if (!isset($path['q'])) {
         die(RestUtils::sendResponse(404));
     }
     $path_array = explode('/', $path['q']);
     if (!isset($path_array[0])) {
         die(RestUtils::sendResponse(404));
     }
     // Set the controller.
     $controller = RestUtils::getController($response, $path_array);
     $controller->process();
     // TODO. Right now we only return json.
     $body = json_encode($controller->getProcessedResponse());
     RestUtils::sendResponse(200, $body, 'application/json');
 }
  private static function getController(RestResponse $response, $path_array) {
    // Check versions of the api.
    if ('v1' != $path_array[0]) {
      die(RestUtils::sendResponse(501));
    }
    try {
      if ('tag' == $path_array[1]) {
        include './tagger/Tagger.php';
        $tagger = Tagger::getTagger();
        if ($response->getRequestVars('url')) {
          $text = file_get_contents($response->getRequestVars('url'));
        } else if ($response->getRequestVars('text')) {
          $text = $response->getRequestVars('text');
        } else {
          RestUtils::sendResponse(500, 'Missing argument: text or url');
        }
        
        $configuration = $tagger->getConfiguration();

        if (empty($configuration['vocab_names'])) {
          RestUtils::sendResponse(500, 'No configured vocabs');
        }

        return $tagger->tagText(
            $text,
            $configuration['vocab_names'],
            $response->getRequestVars('disambiguate') ? true : false,
            $response->getRequestVars('uris') ? true : false,
            $response->getRequestVars('unmatched') ? true : false,
            $response->getRequestVars('markup') ? true : false,
            $response->getRequestVars('nl2br') ? true : false
          );
      }
    }
    catch (Exception $e) {
      die(RestUtils::sendResponse(400));
    }
    die(RestUtils::sendResponse(404));
  }
 public function __construct(RestResponse $response)
 {
     global $conf;
     if (!isset($conf['vocab_names']) || empty($conf['vocab_names'])) {
         throw new ErrorException('Missing vocab definition in configuration.');
     }
     $this->response = $response;
     $this->text = $response->getRequestVars('text');
     if (empty($this->text)) {
         $url = $response->getRequestVars('url');
         if (!empty($url)) {
             // Suppress errors.
             $this->text = @file_get_contents($response->getRequestVars('url'));
         }
     }
     if (empty($this->text)) {
         throw new InvalidArgumentException('No text to find tags in has been supplied.');
     }
     if (mb_detect_encoding($this->text) != 'UTF-8') {
         $this->text = utf8_encode($this->text);
     }
     $ner = $response->getRequestVars('ner');
     if (!empty($ner) && preg_match_all('/(' . implode('|', $conf['vocab_names']) . ')+[\\ ]?/', $ner, $matches)) {
         $this->ner_vocabs = array_intersect_key(array_flip($conf['vocab_names']), array_flip($matches[1]));
     } else {
         $this->ner_vocabs = array_flip($conf['vocab_names']);
     }
     if ($response->getRequestVars('disambiguate')) {
         $this->disambiguate = $response->getRequestVars('disambiguate');
     }
     if ($response->getRequestVars('uris')) {
         $this->return_uris = $response->getRequestVars('uris');
     }
     if ($response->getRequestVars('unmatched')) {
         $this->return_unmatched = $response->getRequestVars('unmatched');
     }
     $this->use_markup = $response->getRequestVars('markup');
     $this->nl2br = $response->getRequestVars('nl2br');
 }