Exemple #1
0
 /**
  * Perform an interaction of a URL.
  *
  * @param   string  $url        A URL.
  * @param   string  $auth       Authentication username and password. (optional)
  * @param   string  $pxhost     The host name of a proxy.
  *                              If it is `null', it is not used. (optional)
  * @param   int     $pxport     The port number of the proxy. (optional)
  * @param   int     $outsec     Timeout in seconds.
  *                              If it is negative, it is not used. (optional)
  * @param   array   $reqheads   An array of extension headers.
  *                              If it is `null', it is not used. (optional)
  * @param   string  $reqbody    The pointer of the entitiy body of request.
  *                              If it is `null', "GET" method is used. (optional)
  * @return  object  Services_HyperEstraier_HttpResponse
  *                  An object into which headers and the entity body
  *                  of response are stored. On error, returns false.
  * @throws  InvalidArgumentException, RuntimeException
  * @access  public
  * @static
  * @ignore
  */
 public static function shuttleUrl($url, $auth = null, $pxhost = null, $pxport = null, $outsec = -1, $reqheads = null, $reqbody = null)
 {
     if (!preg_match('@^(https?://)(\\w.+)@', $url, $matches)) {
         throw new InvalidArgumentException('Invalid URL given.');
     }
     if (is_null($reqheads)) {
         $reqheads = array();
     }
     // set request parameters
     $params = array('http' => array());
     if (is_null($reqbody)) {
         $params['http']['method'] = 'GET';
     } else {
         $params['http']['method'] = 'POST';
         $params['http']['content'] = $reqbody;
         $reqheads['content-length'] = strlen($reqbody);
     }
     if (!is_null($pxhost) && !is_null($pxport)) {
         $params['http']['proxy'] = sprintf('tcp://%s:%d', $pxhost, $pxport);
     }
     $reqheads['user-agent'] = sprintf('Services_HyperEstraier/%s (PHP %s)', SERVICES_HYPERESTRAIER_VERSION, PHP_VERSION);
     if ($auth) {
         $url = $matches[1] . $auth . '@' . $matches[2];
     }
     $params['http']['header'] = '';
     foreach ($reqheads as $key => $value) {
         $params['http']['header'] .= sprintf("%s: %s\r\n", $key, $value);
     }
     $context = stream_context_create($params);
     try {
         // open a stream and send the request
         set_error_handler('services_hyperestraier_utility_open_url_error_handler', E_WARNING);
         $fp = fopen($url, 'r', false, $context);
         restore_error_handler();
         if ($outsec >= 0) {
             stream_set_timeout($fp, $outsec);
         }
         // get the response body
         $body = stream_get_contents($fp);
         // parse the response headers
         $meta_data = stream_get_meta_data($fp);
         if (!empty($meta_data['timed_out'])) {
             fclose($fp);
             throw new RuntimeException('Connection timed out.', Services_HyperEstraier_Error::CONNECTION_TIMEDOUT);
         }
         if (strcasecmp($meta_data['wrapper_type'], 'cURL') == 0) {
             $raw_headers = $meta_data['wrapper_data']['headers'];
         } else {
             $raw_headers = $meta_data['wrapper_data'];
         }
         $http_status = array_shift($raw_headers);
         if (!preg_match('!^HTTP/(.+?) (\\d+) ?(.*)!', $http_status, $matches)) {
             fclose($fp);
             throw new RuntimeException('Malformed response.', Services_HyperEstraier_Error::MALFORMED_RESPONSE);
         }
         $code = (int) $matches[2];
         $headers = array();
         foreach ($raw_headers as $header) {
             list($name, $value) = explode(':', $header, 2);
             $headers[strtolower($name)] = ltrim($value);
         }
         // close the stream
         fclose($fp);
         return new Services_HyperEstraier_HttpResponse($code, $headers, $body);
     } catch (RuntimeException $e) {
         restore_error_handler();
         if ($e->getCode() == Services_HyperEstraier_Error::HTTP_NOT_2XX) {
             return new Services_HyperEstraier_HttpResponse((int) $e->getMessage(), array(), '');
         }
         Services_HyperEstraier_Error::push($e->getCode(), $e->getMessage(), 'exception', $params, array('exception' => $e));
         return false;
     }
 }
$node = new Services_HyperEstraier_Node();
$node->setUrl($uri);
// create a search condition object
$cond = new Services_HyperEstraier_Condition();
$cond->setPhrase('water AND mind');
$cond->setMax(10);
$cond->setSkip(0);
// get the result of search
$nres = $node->search($cond, 0);
if ($nres) {
    if ($nres->docNum() == 0) {
        fprintf(STDOUT, "%s: not found.\n", $cond->getPhrase());
    } else {
        foreach ($nres as $rdoc) {
            // display attributes
            if (($value = $rdoc->getAttribute('@uri')) !== null) {
                fprintf(STDOUT, "URI: %s\n", $value);
            }
            if (($value = $rdoc->getAttribute('@title')) !== null) {
                fprintf(STDOUT, "Title: %s\n", $value);
            }
            // display the snippet text (with property overloading)
            fprintf(STDOUT, "%s", $rdoc->snippet);
        }
    }
} else {
    fprintf(STDERR, "error: %d\n", $node->status);
    if (Services_HyperEstraier_Error::hasErrors()) {
        fputs(STDERR, print_r(Services_HyperEstraier_Error::getErrors(), true));
    }
}