本文整理汇总了PHP中EasyRdf_Format::getHttpAcceptHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP EasyRdf_Format::getHttpAcceptHeader方法的具体用法?PHP EasyRdf_Format::getHttpAcceptHeader怎么用?PHP EasyRdf_Format::getHttpAcceptHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EasyRdf_Format
的用法示例。
在下文中一共展示了EasyRdf_Format::getHttpAcceptHeader方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: load
/**
* Load RDF data into the graph.
*
* If a URI is supplied, but no data then the data will
* be fetched from the URI.
*
* The document type is optional and can be specified if it
* can't be guessed or got from the HTTP headers.
*
* @param string $uri The URI of the graph
* @param string $data Data for the graph
* @param string $format The document type of the data
*/
public function load($uri, $data = null, $format = null)
{
if (!is_string($uri) or $uri == null or $uri == '') {
throw new InvalidArgumentException("\$uri should be a string and cannot be null or empty");
}
if (!$data) {
# No data was given - try and fetch data from URI
# FIXME: prevent loading the same URI multiple times
$client = self::getHttpClient();
$client->setUri($uri);
$client->setHeaders('Accept', EasyRdf_Format::getHttpAcceptHeader());
$response = $client->request();
if (!$response->isSuccessful()) {
throw new EasyRdf_Exception("HTTP request for {$uri} failed: " . $response->getMessage());
}
$data = $response->getBody();
if (!$format) {
$format = $response->getHeader('Content-Type');
$format = preg_replace('/;(.+)$/', '', $format);
}
}
# Guess the format if it is Unknown
if (!$format) {
$format = EasyRdf_Format::guessFormat($data);
}
if (!$format) {
throw new EasyRdf_Exception("Unable to load data of an unknown format.");
}
# Parse the data
$format = EasyRdf_Format::getFormat($format);
$parser = $format->newParser();
return $parser->parse($this, $data, $format, $uri);
}
示例2: query
/** 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()) {
$type = $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());
}
}
示例3: load
/**
* Load RDF data into the graph.
*
* If a URI is supplied, but no data then the data will
* be fetched from the URI.
*
* The document type is optional and can be specified if it
* can't be guessed or got from the HTTP headers.
*
* @param string $uri The URI of the data to load
* @param string $data Optional data for the graph
* @param string $format Optional format of the data
*/
public function load($uri = null, $data = null, $format = null)
{
$this->checkResourceParam($uri, true);
if (!$uri) {
throw new EasyRdf_Exception("No URI given to load() and the graph does not have a URI.");
}
if (!$data) {
# No data was given - try and fetch data from URI
# FIXME: prevent loading the same URI multiple times
$client = EasyRdf_Http::getDefaultHttpClient();
$client->resetParameters(true);
$client->setUri($uri);
$client->setMethod('GET');
$client->setHeaders('Accept', EasyRdf_Format::getHttpAcceptHeader());
$response = $client->request();
if (!$response->isSuccessful()) {
throw new EasyRdf_Exception("HTTP request for {$uri} failed: " . $response->getMessage());
}
$data = $response->getBody();
if (!$format) {
$format = $response->getHeader('Content-Type');
$format = preg_replace('/;(.+)$/', '', $format);
}
}
// Parse the data
return $this->parse($data, $format, $uri);
}
示例4: load
/**
* Load RDF data into the graph from a URI.
*
* If no URI is given, then the URI of the graph will be used.
*
* The document type is optional but should be specified if it
* can't be guessed or got from the HTTP headers.
*
* @param string $uri The URI of the data to load
* @param string $format Optional format of the data (eg. rdfxml)
* @return integer The number of triples added to the graph
*/
public function load($uri = null, $format = null)
{
$this->checkResourceParam($uri, true);
if (!$uri) {
throw new EasyRdf_Exception("No URI given to load() and the graph does not have a URI.");
}
// Setup the HTTP client
$client = EasyRdf_Http::getDefaultHttpClient();
$client->resetParameters(true);
$client->setConfig(array('maxredirects' => 0));
$client->setMethod('GET');
$client->setHeaders('Accept', EasyRdf_Format::getHttpAcceptHeader());
$requestUrl = $uri;
$response = null;
$redirectCounter = 0;
do {
// Have we already loaded it into the graph?
$requestUrl = EasyRdf_Utils::removeFragmentFromUri($requestUrl);
if (in_array($requestUrl, $this->loaded)) {
return 0;
}
// Make the HTTP request
$client->setHeaders('host', null);
$client->setUri($requestUrl);
$response = $client->request();
// Add the URL to the list of URLs loaded
$this->loaded[] = $requestUrl;
if ($response->isRedirect() and $location = $response->getHeader('location')) {
// Avoid problems with buggy servers that add whitespace
$location = trim($location);
// Some servers return relative URLs in the location header
// resolve it in relation to previous request
$baseUri = new EasyRdf_ParsedUri($requestUrl);
$requestUrl = $baseUri->resolve($location)->toString();
$requestUrl = EasyRdf_Utils::removeFragmentFromUri($requestUrl);
// If it is a 303 then drop the parameters
if ($response->getStatus() == 303) {
$client->resetParameters();
}
++$redirectCounter;
} elseif ($response->isSuccessful()) {
// If we didn't get any location, stop redirecting
break;
} else {
throw new EasyRdf_Http_Exception("HTTP request for {$requestUrl} failed: " . $response->getMessage(), $response->getStatus(), null, $response->getBody());
}
} while ($redirectCounter < $this->maxRedirects);
if (!$format or $format == 'guess') {
list($format, $params) = EasyRdf_Utils::parseMimeType($response->getHeader('Content-Type'));
}
// Parse the data
return $this->parse($response->getBody(), $format, $uri);
}
示例5: testGetHttpAcceptHeader
public function testGetHttpAcceptHeader()
{
$accept = explode(',', EasyRdf_Format::getHttpAcceptHeader());
$this->assertContains('application/json', $accept);
}
示例6: testGetHttpAcceptHeaderWithExtra
public function testGetHttpAcceptHeaderWithExtra()
{
$accept = EasyRdf_Format::getHttpAcceptHeader(array('extra/header' => 0.5));
$this->assertContains('application/json', $accept);
$this->assertContains('extra/header;q=0.5', $accept);
}
示例7: request
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());
}
}
示例8: testGetHttpAcceptHeaderLocale
public function testGetHttpAcceptHeaderLocale()
{
$current_locale = setlocale(LC_NUMERIC, 0);
setlocale(LC_NUMERIC, 'fi_FI.UTF-8');
$accept = EasyRdf_Format::getHttpAcceptHeader(array('extra/header' => 0.5));
$this->assertContains('extra/header;q=0.5', $accept);
setlocale(LC_NUMERIC, $current_locale);
}