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.
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;
?>
Related
I'm trying to use the discogs php api in a PHP script to get info on a release using the release ID. For example, when I make a request to:
http://mywebsite.com/test.php?id=1017868
I want to call the discogs API to get info on the release with id = 1017868. I can see the info I want by manually going to:
https://api.discogs.com/releases/1017868
So I have my $consumerKey =and $consumerSecret and I'm following the DISCOGS AUTH FLOW instructions, which says I can send my keys in a get request like so:
curl "https://api.discogs.com/database/search?q=Nirvana" -H "Authorization: Discogs key=foo123, secret=bar456"
I'm trying to make a get request for my target id in my php script like so:
<?php
echo "hello world <br>";
//discogs simple auth flow, http requests
$remote_url = 'https://api.discogs.com/releases/1017868';
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => "Authorization: Discogs key=mykeyasdlkhaskld, secret=mysecretkeykjnasdkjnsadkj"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents($remote_url, false, $context);
print($file);
echo "end of program";
?>
But I keep getting the error:
"failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden".
Is there something I'm forgetting for making my request to the server? Thanks.
I have made the same request in Postman and it gives me the correct response, but when i make the same call from my website it crashes.
Im simply trying to make a GET request from PHP with one header, the oauth2 authorization token.
Code (googleLogin.php):
<?php
$url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Authorization: Bearer " . $_GET["access_token"] . "\r\n"
)
);
$context = stream_context_create($options);
$file = file_get_contents($url, false, $context);
print_r($file);
?>
Error:
PHP Warning:
file_get_contents(https://www.googleapis.com/oauth2/v1/userinfo?alt=json):
failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
in /home/shawn/public_html/cloud/php/googleLogin.php on line 14
I dont know if this error implies (i) The file googleLogin.php on the web server doesnt have correct permissions (i tried 777), (ii) The access token is expired/invalid (it cant be because i tried the same token 2 seconds later successfully with Postman), or (iii) php is formatting my HTTP request incorrectly
I'm trying to scrape data from some websites. For several sites it all seems to go fine, but for one website it doesn't seem to be able to get any HTML. This is my code:
<?php include_once('simple_html_dom.php');
$html = file_get_html('https://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=' . $_POST['data']);
echo $html; ?>
I'm using ajax to fetch the data. When I log the returned value in my js it's completely empty.
Could it be due to the fact that this website is running on https? And if so, is there any way to work around it? (I've tried changed the url to http, but I get the same result)
Update:
If I var_dump the $html variable, I get bool(false).
My PHP error log says this:
[27-Feb-2014 22:20:50 Europe/Amsterdam] PHP Warning: file_get_contents(http://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=tarmogoyf): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
in /Users/leondewit/PhpstormProjects/Magic/stores/simple_html_dom.php on line 75
It's your user agent, file_get_contents doesn't send one by default, so:
$url = 'http://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=tarmogoyf';
$context = stream_context_create(array('http' => array('header' => 'User-Agent: Mozilla compatible')));
$response = file_get_contents($url, false, $context);
$html = str_get_html($response);
echo $html;
function modify(.......)
{
$mcontact = file_get_contents( "https://test.httpapi.com/api/contacts/modify.json?auth-userid=$uid&auth-password=$pass&contact-id=$cid&name=$name &company=$company&email=$email&address-line-1=$street&city=$city&country=$country&zipcode=$pincode&phone-cc=$countryCodeList[$phc]&phone=$phone" );
$mdetails = json_decode( $mcontact, true );
return $mdetails;
}
using this modify function, displays warning mesage
Warning: file_get_contents(https://...#hihfg.com&address-line-1=3,dfgdf,fgdf&city=dfgfd&country=India&zipcode=641005&phone-cc=91&phone=756657)
[function.file-get-contents]: failed to open stream: HTTP request failed!
HTTP/1.0 400 Bad request in /home/gfdgfd/public_html/new_one/customer/account/class.whois.php
on line 49
Please help me, modify contact details..
HTTP/1.0 400 Bad request - everything you need to know: You are sending a request the server doesn't like.
Are you sure it accepts a GET request and doesn't require POST? Is the URL correct?
Your URL is wrong. You have an additional space character: &name=$name &company=$company. Remove it.
I am having a problem with PHP's file_get_contents command.
$url = "http://api.rememberthemilk.com/services/rest/".$format.$auth_token.$filter."&api_sig=".$md5.$apikey.$method;
$content = file_get_contents($url);
$array = json_decode($content, true);
$taskname = $array['rsp']['tasks']['list']['taskseries']['name'];
$duedate = $array['rsp']['tasks']['list']['taskseries']['task']['due'];
($format, $auth_token, $filter, $md5, $apikey, $method are already defined in the script)
When I try to run this code this error is returned:
[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad request for line 101
line 101 = $content, = file_get_contents($url);
How to fix this? Thanks!!!
This url does not look great.
http://api.rememberthemilk.com/services/rest/?format=json&auth_token=AUTH_TOKEN&filter=dueWithin:"3 days of today"&api_sig=API_SIG&api_key=API_KEY&method=rtm.tasks.getList
Encode the tokens as follows:
$filter = 'filter='.urlencode( 'dueWithin:"3 days of today"' );
Use urlencode().
Try printing the URL after concatenating the variables. Then paste the URL into the address bar of your browser and see what comes back. Because it's a web service call, your browser might not know what to do with the response. In that case you might get additional information using the command-line user agent "curl", e.g.
curl -v 'http://some-url'
curl is built in to Macs and other *nix machines and is also available for Windows.