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
}
Related
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"];
}
When I click on inline keyboard, I can not get no answer our .Help me please? This is my code:
if ($data == "1") {
$message = 's';
$url = 'https://api.telegram.org/bot'.$token.'/sendMessage?chat_id='.$user_id.'&text='.$message;
$update = file_get_contents($url);
$url = 'https://api.telegram.org/bot'.$token.'/answerCallbackQuery?chat_id='.$user_id .'&callback_query_id='.$chat_id_in;
$update = file_get_contents($url);
}
$error = error_get_last();
echo "HTTP request failed. Error was: " . $error['message'];
You will see HTTP status code. Something like this :
HTTP request failed! HTTP/1.1 404 Not Found
I advise you not to use file_get_content and use CURL (guzzle or other)
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.
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.
I have been trying to retrieve the following data using the following code in php:
$fql_query='SELECT%20name,%20birthday_date,%20sex,%20current_location.name,%20email%20FROM%20user%20WHERE%20uid=me()';<br>
// Run fql query<br>
$fql_query_url = 'https://graph.facebook.com/'
. '/fql?q='.$fql_query
. '&' . $access_token;<br>
$fql_query_result = file_get_contents($fql_query_url);<br>
$fql_query_obj = json_decode($fql_query_result, true);<br>
Warning:
file_get_contents(https://graph.facebook.com//fql?q=SELECT%20name,%20birthday_date,%20sex,%20current_location.name,%20email%20FROM%20user%20WHERE%20uid=me()&access_token=)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.0 400 Bad Request in on line 223
I believe the following error is because the user has not provided current location in the profile. If I add the data the code works fine. How do I catch that error?
"[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in on line 223"
Would tell you that as the function is failing, it can be tested logically for a result e.g.
$fql_query='SELECT%20name,%20birthday_date,%20sex,%20current_location.name,%20email%20FROM%20user%20WHERE%20uid=me()';
// Check FQL query
$fql_query_url = 'https://graph.facebook.com/'
. '/fql?q='.$fql_query
. '&' . $access_token;<br>
// First logically check the result, TRUE: Result is passed to $fql_query_result.
// FALSE: $flq_query_result = 0 or false.
$fql_query_result = file_get_contents($fql_query_url) ? file_get_contents($fql_query_url) : 0;
// Only json_decode the result if it was valid.
$fql_query_obj = (!$fql_query_result == 0) ? json_decode($fql_query_result, true) : "FQL was not a valid query";