Receiving HTTP header 406 error

半世苍凉 提交于 2019-12-11 20:22:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!