1

im working on an app which uses api calls. When im typing the right api keys everything works fine howver if i put wrong details i will get the 401 error. I know what this error means an im expecting it so i have my own handlers, the problem is the warning which the browser outpus. I've tried to disable every error but then i cant debug the app, Is it possible to disable it from php's config? If yes how? Thanks

Code:

$apicalldata = file_get_contents("https://api.digitalocean.com/v1/droplets/?client_id=xxx&api_key=xxx");

$call = json_decode($apicalldata);


$status = $call->status;
if ($status != "OK") {
echo "Sorry, the API details are wrong!";
}
vonUbisch
  • 1,384
  • 17
  • 32
  • What warning does the browser output? I don't understand. Is it verbose for DigitalOceans errors, or your own? – ʰᵈˑ May 14 '14 at 14:26
  • This code is correct. What is your exact problem? – Ravi Dhoriya ツ May 14 '14 at 14:27
  • I told you. The browser outputs the 401 error. http://prntscr.com/3j50tu –  May 14 '14 at 14:28
  • 1
    @MeletisFlevarakis this has nothing to do with PHP. It's most likely HTTP 401 -> Unauthorized – LifeQuery May 14 '14 at 14:29
  • @LifeQuery That's the whole point. The OP wants to avoid the warning message that's automatically printed and instead handle the error himself. – Carsten May 14 '14 at 14:31
  • @Carsten Have a look at the title. 401 has nothing to do with PHP! – LifeQuery May 14 '14 at 14:33
  • @Carsten read both queastion carefully. Then read them again. Is it the same? –  May 14 '14 at 14:37
  • @LifeQuery thats right. it hasnt to do with php but you can disable every error with php so i assumed that php has something for that –  May 14 '14 at 14:38
  • @MeletisFlevarakis Not exactly. The other question is trying to catch a warning caused by a HTTP 404. I'm confident you'll manage to adapt that to your situation. Especially since the process of suppressing the warning is the same. – Carsten May 14 '14 at 14:39
  • 1
    @LifeQuery _In this case_ it has. The OP wants to access an API which returns a 401 if wrong credentials are entered. OP knows this, but notices that `file_get_contents` will throw a _warning_ with associated error message if it encounters a 401. He doesn't want that warning to show, and instead handle the error himself (by displaying a custom message for the user, for example). So he asks here how to suppress this warning message. At least that's what I thought this question was about. – Carsten May 14 '14 at 14:41

2 Answers2

0

I'd suggest using a Try Catch syntax

PHP Try Catch Docs

try{
   $apicalldata = file_get_contents("https://api.digitalocean.com/v1/droplets/?client_id=xxx&api_key=xxx");

} catch (exception $e) {
 /*** bad return (401)***/
   echo "Sorry, the API details are wrong!";
}  
  /*** good return ***/
  doSomething()
Ravi Dhoriya ツ
  • 4,435
  • 8
  • 37
  • 48
cs45977
  • 400
  • 3
  • 11
0

Just put @file_get_contents This will hide the error!

user3535778
  • 135
  • 2
  • 15