Print API error messages with PHP
It seems that file_get_contents() fails as soon as it gets the HTTP/403 and doesn’t receive the content.
The function returns the read data or FALSE on failure.
Try using cURL instead.
See: https://github.com/codemasher/gw2-database/blob/master/classes/gw2api.class.php#L130
Or you could use my API wrapper that throws a AuthenticationException with a correct reason. https://github.com/gw2treasures/gw2api
(I have to add better documentation for that still)
—
Your example would be:
$api = new GW2Api()
try {
$characters = $api->characters('api_key')->all();
} catch(AuthenticationException $exception) {
// echo error message
echo $exception->getMessage();
}
(edited by darthmaim.6017)
It seems that file_get_contents() fails as soon as it gets the HTTP/403 and doesn’t receive the content.
The function returns the read data or FALSE on failure.
Try using cURL instead.
See: https://github.com/codemasher/gw2-database/blob/master/classes/gw2api.class.php#L130
Thank you, I have been using cURL to no avail but after looking at your link noticed some missing constants, and a ‘CA root certificate file’ that was needed to .. make the SSL work? I don’t know much about that, but I’m happy with the results. The working code is the following:
$url = “https://api.guildwars2.com/v2/characters”;
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => getcwd().“\cacert.pem”,
CURLOPT_RETURNTRANSFER => true]);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
I got the certificate from here http://curl.haxx.se/ca/cacert.pem
Thanks for your guidance, this was driving me nuts.
Or you could use my API wrapper that throws a AuthenticationException with a correct reason. https://github.com/gw2treasures/gw2api
(I have to add better documentation for that still)
—
Your example would be:
$api = new GW2Api() try { $characters = $api->characters('api_key')->all(); } catch(AuthenticationException $exception) { // echo error message echo $exception->getMessage(); }
I’m aware about wrappers, but there are so many on the wiki and I couldn’t decide on which one. Since you are offering yours, I will assume yours is the best and most up to date and will gladly use it once I finish understanding this thing called API
Hello, I’m not able to print error messages like
“requires scope characters” or
“endpoint requires authentication” with PHP. If anyone can help me with this I’ll be grateful. I have the following code:$apiV2 = “https://api.guildwars2.com/v2/characters”;
$json = file_get_contents($apiV2);
$data = json_decode($json);
echo $data->text;Expected result using that url would be {"text":“endpoint requires authentication”}, however I’m only getting warnings from PHP stating that it ‘failed to open steam: HTTP request failed! 403 forbidden’ and that the $data var is empty.
How could I achieve this? Querying a valid API url display info correctly, but I would like to be able to store and print more accurate error messages in case something goes wrong.
You can check if $data returns null. Right now there’s only 3 use cases, key is invalid, key does not give permission to XYZ and key is valid. You can check if key is valid using the tokeninfo API if it returns null = invalid key and if it returns null on the others but not tokeninfo it means that the key is not allowed to view XYZ.
This assumes you want to stick with file_get_contents and is kinda a workaround. Otherwise, use cURL for the best results.
It seems that file_get_contents() fails as soon as it gets the HTTP/403 and doesn’t receive the content.
The function returns the read data or FALSE on failure.
Try using cURL instead.
See: https://github.com/codemasher/gw2-database/blob/master/classes/gw2api.class.php#L130Thank you, I have been using cURL to no avail but after looking at your link noticed some missing constants, and a ‘CA root certificate file’ that was needed to .. make the SSL work? I don’t know much about that, but I’m happy with the results. The working code is the following:
$url = “https://api.guildwars2.com/v2/characters”;
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => getcwd().“\cacert.pem”,
CURLOPT_RETURNTRANSFER => true]);
$output = curl_exec($ch);
curl_close($ch);
echo $output;I got the certificate from here http://curl.haxx.se/ca/cacert.pem
Thanks for your guidance, this was driving me nuts.
The missing variables are all in the code i linked, along with a link to the root certificates (which are indeed needed to “make SSL work”)
https://github.com/codemasher/gw2-database/blob/master/classes/gw2api.class.php#L65
Glad you found it anyway and it worked for you!
You can check if $data returns null.
It won’t return null. Ever. It will return a boolean false or a string containing the response (which may be empty) – that makes a slight difference.
(edited by smiley.1438)
curl is the right way to go, you can check the status code and the potential error message to determine if the result is ok. I also recommend guzzle on top of curl, it’s easy to make multiple parallel requests (instead of working with curl_multi directly).
I’m aware about wrappers, but there are so many on the wiki and I couldn’t decide on which one. Since you are offering yours, I will assume yours is the best and most up to date and will gladly use it once I finish understanding this thing called API
Doing it yourself first is a great way to learn how it works! But we don’t have to make it harder on ourselves by reinventing the wheel every time
I really don’t want to advertise my wrapper, but most wrappers on the wiki are still using the old v1 API and/or don’t support the authenticated endpoints. I’d say try some out and use the one that you like the most. (I will throw some more documentation with some better examples for my wrapper up over the weekend)
I will throw some more documentation with some better examples for my wrapper up over the weekend
Took a little bit longer, but its there now: https://github.com/GW2Treasures/gw2api#usage