uri = $uri; } /** Get the URI of the SPARQL endpoint * * @return string The URI of the SPARQL endpoint */ public function getUri() { return $this->uri; } /** Make a query to the SPARQL endpoint * * SELECT and ASK queries will return an object of type * EasyRdf_Sparql_Result. * * CONSTRUCT and DESCRIBE queries will return an object * of type EasyRdf_Graph. * * @param string $query The query string to be executed * @return object EasyRdf_Sparql_Result|EasyRdf_Graph Result of the query. */ public function query($query) { # Add namespaces to the queryString $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(); $client->setUri($this->uri); $client->setMethod('GET'); $accept = EasyRdf_Format::getHttpAcceptHeader( array( 'application/sparql-results+json' => 1.0, 'application/sparql-results+xml' => 0.8 ) ); $client->setHeaders('Accept', $accept); $client->setParameterGet('query', $prefixes . $query); $response = $client->request(); if ($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->uri, $response->getBody(), $type); } } else { throw new EasyRdf_Exception( "HTTP request for SPARQL query failed: ".$response->getBody() ); } } /** Magic method to return URI of the SPARQL endpoint when casted to string * * @return string The URI of the SPARQL endpoint */ public function __toString() { return $this->uri == null ? '' : $this->uri; } }