问题
I am trying to invoke http://dbpedia.org/page/Los_Angeles in PHP with HTTP headers for an educational assignment I have been given. It must be this URL i.e. I am not allowed to use the JSON URL directly.
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Accept: application/json"
)
);
$context = stream_context_create($opts);
$url = "http://dbpedia.org/page/Los_Angeles";
$data = file_get_contents($url,false,$context);
echo $data;
?>
I'm facing this error:
Warning: file_get_contents(http://dbpedia.org/page/Los_Angeles): failed to open stream: HTTP request failed! HTTP/1.1 406 Unacceptable.
Please help to get rid of this error. Note: I read about this error and found suggestions to use CURL. However, I don't want to go into installation of CURL. Kindly suggest using file_get_contents
.
回答1:
406 says that application/json is not available.
From Wikipedia:
406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.
<?php
$url = "http://dbpedia.org/page/Los_Angeles";
$data = file_get_contents($url);
echo $data;
?>
来源:https://stackoverflow.com/questions/19624407/receiving-http-header-406-error