You are here

protected function EasyRdf_Sparql_Client::request in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Client.php \EasyRdf_Sparql_Client::request()
2 calls to EasyRdf_Sparql_Client::request()
EasyRdf_Sparql_Client::query in vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Client.php
Make a query to the SPARQL endpoint
EasyRdf_Sparql_Client::update in vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Client.php
Make an update request to the SPARQL endpoint

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Client.php, line 230

Class

EasyRdf_Sparql_Client
Class for making SPARQL queries using the SPARQL 1.1 Protocol

Code

protected function request($type, $query) {

  // Check for undefined prefixes
  $prefixes = '';
  foreach (EasyRdf_Namespace::namespaces() as $prefix => $uri) {
    if (strpos($query, "{$prefix}:") !== false and strpos($query, "PREFIX {$prefix}:") === false) {
      $prefixes .= "PREFIX {$prefix}: <{$uri}>\n";
    }
  }
  $client = EasyRdf_Http::getDefaultHttpClient();
  $client
    ->resetParameters();

  // Tell the server which response formats we can parse
  $accept = EasyRdf_Format::getHttpAcceptHeader(array(
    'application/sparql-results+json' => 1.0,
    'application/sparql-results+xml' => 0.8,
  ));
  $client
    ->setHeaders('Accept', $accept);
  if ($type == 'update') {
    $client
      ->setMethod('POST');
    $client
      ->setUri($this->updateUri);
    $client
      ->setRawData($prefixes . $query);
    $client
      ->setHeaders('Content-Type', 'application/sparql-update');
  }
  elseif ($type == 'query') {

    // Use GET if the query is less than 2kB
    // 2046 = 2kB minus 1 for '?' and 1 for NULL-terminated string on server
    $encodedQuery = 'query=' . urlencode($prefixes . $query);
    if (strlen($encodedQuery) + strlen($this->queryUri) <= 2046) {
      $delimiter = $this->queryUri_has_params ? '&' : '?';
      $client
        ->setMethod('GET');
      $client
        ->setUri($this->queryUri . $delimiter . $encodedQuery);
    }
    else {

      // Fall back to POST instead (which is un-cacheable)
      $client
        ->setMethod('POST');
      $client
        ->setUri($this->queryUri);
      $client
        ->setRawData($encodedQuery);
      $client
        ->setHeaders('Content-Type', 'application/x-www-form-urlencoded');
    }
  }
  $response = $client
    ->request();
  if ($response
    ->getStatus() == 204) {

    // No content
    return $response;
  }
  elseif ($response
    ->isSuccessful()) {
    list($type, $params) = EasyRdf_Utils::parseMimeType($response
      ->getHeader('Content-Type'));
    if (strpos($type, 'application/sparql-results') === 0) {
      return new EasyRdf_Sparql_Result($response
        ->getBody(), $type);
    }
    else {
      return new EasyRdf_Graph($this->queryUri, $response
        ->getBody(), $type);
    }
  }
  else {
    throw new EasyRdf_Exception("HTTP request for SPARQL query failed: " . $response
      ->getBody());
  }
}