File_get_content failed to open the stream - php

I have checked previous related threads, but my problem deals with specific readability API.
I am trying to get most relavant image from web page. I am using redhttps://www.readability.com/developers/api/parser for that.
Here is my code:
<?php
define('TOKEN', "1b830931777ac7c2ac954e9f0d67df437175e66e");
define('API_URL', "https://www.readability.com/api/content/v1/parser?url=%s&token=%s");
function get_image($url) {
// sanitize it so we don't break our api url
$encodedUrl = urlencode($url);
$TOKEN = '1b830931777ac7c2ac954e9f0d67df437175e66e';
$API_URL = 'https://www.readability.com/api/content/v1/parser?url=%s&token=%s';
// $API_URL = 'http://blog.readability.com/2011/02/step-up-be-heard-readability-ideas';
// build our url
$url = sprintf($API_URL, $encodedUrl, $TOKEN);
// call the api
$response = file_get_contents($url);
if( $response ) {
return false;
}
$json = json_decode($response);
if(!isset($json['lead_image_url'])) {
return false;
}
return $json['lead_image_url'];
}
echo get_image('https://www.facebook.com/');
?>
Error:
Warning: file_get_contents(https://www.readability.com/api/content/v1/parser?url=https%3A%2F%2Fwww.facebook.com%2F&token=1b830931777ac7c2ac954e9f0d67df437175e66e): failed to open stream: HTTP request failed! HTTP/1.1 403 FORBIDDEN in F:\wamp\www\inviteold\test2.php on line 16

You seem to be getting content from Facebook.
Due to your error 403 FORBIDDEN, I would say that your parser/code is not authorised to access the facebook page you are accessing and thus you are getting denied due to privacy settings.

Related

How to handle 500 errors from Google API

How can I handle google API errors? For example, the following script will get the screenshot from the website requested:
$api_response = file_get_contents("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" . $websiteURL . "&screenshot=true");
$result = json_decode($api_response, true);
$screenshot1 = $result['lighthouseResult']['audits']['final-screenshot']['details']['data'];
but if the domain doesn't exists google returns this error:
Failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
How can I handle this error and create a custom error page for that?
My project is on Laravel 8.
You could do it with an if statement
if(($api_response = #file_get_contents("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" . $websiteURL . "&screenshot=true")) !== false) {
//success
$result = json_decode($api_response, true);
$screenshot1 = $result['lighthouseResult']['audits']['final-screenshot']['details']['data'];
}
else{
//do error stuff
}

How to check if API is able to handle the request using PHP

I have a PHP file calling a request to an API:
$url = https://api.spoonacular.com/recipes/search?query=spaghetti%bolognese&apiKey={{apikey}}3;
$response = json_decode(file_get_contents($url), true);
$name = $response["title"];
The $name should show what the json file's response's value is when the key is title.
I only have 150 requests allowed per day with my account so I am trying to only find $name when it can still make the request. No matter what I try I am still getting the following error:
file_get_contents(https://api.spoonacular.com/recipes/search?query=spaghetti%bolognese&apiKey={{apikey}}):
failed to open stream: HTTP request failed! HTTP/1.1 402
I have tried the following but don't know where I'm going wrong. Can anyone help?
$url = https://api.spoonacular.com/recipes/search?query=spaghetti%bolognese&apiKey={{apikey}}3;
if(file_get_contents($url), true) {
$name = $response["title"];
}

GET Request with params: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

I am trying to use new features available with Steam API and I am having trouble, I think this is more of general issue than actual service related itself.
I keep getting:
failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
I am trying to send GET request with parameters using:
<?php
$url = "https://api.steampowered.com/ICSGOTournaments_730/GetTournamentItems/v1?key=".$_SESSION['steam_appid']."&event=9&steamid=".$_SESSION['steam_steamid']."&steamidkey=".$_SESSION['userAuthCode'];
$response = file_get_contents($url);
echo($response);
?>
I am 100% sure the parameters are correct I think it's something rather to do with my code than Steam itself.
When I try to use same code but different API call:
$url = "https://api.steampowered.com/ICSGOTournaments_730/GetTournamentLayout/v1?key=".$_SESSION['steam_appid']."&event=9";
That executes fine and returns data so it's something related to the other two parameters.
I am wondering is there any problem with the way I am building URL's?
Sessions file (3rd Party):
<?php
if (empty($_SESSION['steam_uptodate']) or empty($_SESSION['steam_personaname'])) {
require 'SteamConfig.php';
$url = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$steamauth['apikey']."&steamids=".$_SESSION['steamid']);
$content = json_decode($url, true);
$_SESSION['steam_appid'] = $steamauth['apikey'];
$_SESSION['steam_steamid'] = $content['response']['players'][0]['steamid'];
$_SESSION['steam_communityvisibilitystate'] = $content['response']['players'][0]['communityvisibilitystate'];
$_SESSION['steam_profilestate'] = $content['response']['players'][0]['profilestate'];
$_SESSION['steam_personaname'] = $content['response']['players'][0]['personaname'];
$_SESSION['steam_lastlogoff'] = $content['response']['players'][0]['lastlogoff'];
$_SESSION['steam_profileurl'] = $content['response']['players'][0]['profileurl'];
$_SESSION['steam_avatar'] = $content['response']['players'][0]['avatar'];
$_SESSION['steam_avatarmedium'] = $content['response']['players'][0]['avatarmedium'];
$_SESSION['steam_avatarfull'] = $content['response']['players'][0]['avatarfull'];
$_SESSION['steam_personastate'] = $content['response']['players'][0]['personastate'];
if (isset($content['response']['players'][0]['realname'])) {
$_SESSION['steam_realname'] = $content['response']['players'][0]['realname'];
} else {
$_SESSION['steam_realname'] = "Real name not given";
}
$_SESSION['steam_primaryclanid'] = $content['response']['players'][0]['primaryclanid'];
$_SESSION['steam_timecreated'] = $content['response']['players'][0]['timecreated'];
$_SESSION['steam_uptodate'] = time();
include 'mysql_connection.php';
$userAuth = getAuth();
if($userAuth != false){
$_SESSION['userAuthCode'] = $userAuth;
}
else{
$_SESSION['userAuthCode'] = "Unset";
}
}
$steamprofile['steamid'] = $_SESSION['steam_steamid'];
$steamprofile['communityvisibilitystate'] = $_SESSION['steam_communityvisibilitystate'];
$steamprofile['profilestate'] = $_SESSION['steam_profilestate'];
$steamprofile['personaname'] = $_SESSION['steam_personaname'];
$steamprofile['lastlogoff'] = $_SESSION['steam_lastlogoff'];
$steamprofile['profileurl'] = $_SESSION['steam_profileurl'];
$steamprofile['avatar'] = $_SESSION['steam_avatar'];
$steamprofile['avatarmedium'] = $_SESSION['steam_avatarmedium'];
$steamprofile['avatarfull'] = $_SESSION['steam_avatarfull'];
$steamprofile['personastate'] = $_SESSION['steam_personastate'];
$steamprofile['realname'] = $_SESSION['steam_realname'];
$steamprofile['primaryclanid'] = $_SESSION['steam_primaryclanid'];
$steamprofile['timecreated'] = $_SESSION['steam_timecreated'];
$steamprofile['uptodate'] = $_SESSION['steam_uptodate'];
?>
file_get_contents - for using https need use:
from php.net
When using SSL, Microsoft IIS will violate the protocol by closing the
connection without sending a close_notify indicator. PHP will report
this as "SSL: Fatal Protocol Error" when you reach the end of the
data. To work around this, the value of error_reporting should be
lowered to a level that does not include warnings. PHP can detect
buggy IIS server software when you open the stream using the https://
wrapper and will suppress the warning. When using fsockopen() to
create an ssl:// socket, the developer is responsible for detecting
and suppressing this warning.

Using readability parser API to fetch image url from web page

Here I found readability which parse the data from web page and give information of interest.
But I could not understand how to use it.
https://www.readability.com/developers/api/parser
Request: GET /api/content/v1/parser?url=http://blog.readability.com/2011/02/step-up-be-heard-readability-ideas/&token=1b830931777ac7c2ac954e9f0d67df437175e66e
It gives json response.
I am working on PHP, how to make above request for particular url like http://google.com?
UPDATE1:
<?php
define('TOKEN', "1b830931777ac7c2ac954e9f0d67df437175e66e");
define('API_URL', "https://www.readability.com/api/content/v1/parser?url=%s&token=%s");
function get_image($url) {
// sanitize it so we don't break our api url
$encodedUrl = urlencode($url);
//$TOKEN = '1b830931777ac7c2ac954e9f0d67df437175e66e';
//Also tried with $API_URL = 'http://blog.readability.com/2011/02/step-up-be-heard-readability-ideas'; with no luck
// build our url
$url = sprintf(API_URL, $encodedUrl, TOKEN); //Also tried with $TOKEN
// call the api
$response = file_get_contents($url);
if( $response ) {
return false;
}
$json = json_decode($response);
if(!isset($json['lead_image_url'])) {
return false;
}
return $json['lead_image_url'];
}
echo get_image('http://nextbigwhat.com/');
?>
Any issue with code? gives error - >
Warning: file_get_contents(https://www.readability.com/api/content/v1/parser?url=http%3A%2F%2Fnextbigwhat.com&token=1b830931777ac7c2ac954e9f0d67df437175e66e): failed to open stream: HTTP request failed! HTTP/1.1 403 FORBIDDEN in F:\wamp\www\inviteold\test2.php on line 14
Perhaps something like this:
define('TOKEN', "your_token_here");
define('API_URL', "https://www.readability.com/api/content/v1/parser?url=%s&token=%s");
function get_image($url) {
// sanitize it so we don't break our api url
$encodedUrl = urlencode($url);
// build our url
$url = sprintf(API_URL, $encodedUrl, $token);
// call the api
$response = file_get_contents($url);
if( $response ) {
return false;
}
$json = json_decode($response);
if(!isset($json['lead_image_url'])) {
return false;
}
return $json['lead_image_url'];
}
The code is not tested so you might need to adjust it.

file_get_contents HTTP request failed! HTTP/1.1 501 Method Not Implemented

I am trying to grab some information from a json ecoded page, however I'm receiving the HTTP/1.1 501 Method Not Implemented error while assigning the value to the variable $data
if(isset($_GET['xblaccount'])) {
$gamertag = htmlspecialchars(strip_tags($_GET['xblaccount']));
$url = 'http://www.xboxleaders.com/api/profile.json?gamertag=' . urlencode($gamertag);
$data = file_get_contents($url);
}
I've also tried
$gamertag = 'kfj32j8fj23f';
And receive the same error
This is an interesting question because the code does work if you use an actual account name (e.g. Stallionh83 or Major Nelson).
Having discovered this I then checked the headers in a browser (FF) which also returned the 501 response header. I assume therefore that this is then a server side response for non-accounts.
Bearing that in mind I suggest editing your code to:
if(isset($_GET['xblaccount'])) {
$gamertag = htmlspecialchars(strip_tags($_GET['xblaccount']));
$url = 'http://www.xboxleaders.com/api/profile.json?gamertag=' . urlencode($gamertag);
$data = #file_get_contents($url); // # to suppress the warning
if($data){
//Account exists...
}
else{
//Account doesn't exist...
}
}
$data will return FALSE if the account doesn't exist, otherwise it will return the JSON string.

Categories