-1

I wanted to circumvent the errors I might encounter in php curl with try / catch, but the cath part does not work, where is the problem ??

<?php
    try {
        $username = $_POST['username'];
        $pass = $_POST['pass'];
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL,"https://example.com/api?username=".$username."&password=".$pass);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $server_output = curl_exec ($ch);
        
        $json = json_decode($server_output, true);
        
        $token = $json["session"]["sessionId"];
        echo $token;
        curl_close ($ch);
        
        
    }
    
    catch(Exception $e){
        echo "Error";
    }
    
    ?>

1 Answers1

0

Find below updated snippet to throw an exeption when token is empty...

   <?php
    try {
        $username = $_POST['username'];
        $pass = $_POST['pass'];
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL,"https://example.com/api?username=".$username."&password=".$pass);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $server_output = curl_exec ($ch);
        
        $json = json_decode($server_output, true);
        
        $token = $json["session"]["sessionId"];

        if(empty($token)){
            throw new Exception('Token not found');
        }
        echo $token;
        curl_close ($ch);
        
        
    }
    
    catch(Exception $e){
        echo "Error";
    }
    
    ?>

I have added below line to make sure that your code catch exception when there is no token...

if(empty($token)){
   throw new Exception('Token not found');
}
Momin IqbalAhmed
  • 950
  • 6
  • 13
  • Well after this code I used a different curl, if there is an error in that part, how do I catch it? – Bilal İnal Jan 25 '21 at 10:51
  • If your curl response will thrown FATAL error then you don't have to do anything. But if your curl request is successful, but unable get any response (response is empty or any other logic that you would like to apply) then you need to manage manually as did in empty token case – Momin IqbalAhmed Jan 25 '21 at 10:53
  • I tried the code you threw, because there is no token value, an error appears in that $ token line. – Bilal İnal Jan 25 '21 at 10:58
  • what is the value/error in $token variable? – Momin IqbalAhmed Jan 25 '21 at 11:01
  • Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\Login_v15\sonuc.php on line 14 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\Login_v15\sonuc.php on line 14 Error – Bilal İnal Jan 25 '21 at 11:07
  • Try this `if (error_reporting()) { throw new Exception( $token); }` after `$token` . Whenever $token thrown an error it will catch exception – Momin IqbalAhmed Jan 25 '21 at 11:11
  • This fatal error came on top of it Fatal error: Uncaught Exception in C:\xampp\htdocs\Login_v15\sonuc.php:17 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Login_v15\sonuc.php on line 17 – Bilal İnal Jan 25 '21 at 11:17
  • @Bilalİnal "Trying to access array offset on value of type null" indicates there was an issue with decoding the response as json, not an issue with curl. Dump your response (before decoding!) and see what you actually got back. – El_Vanja Jan 25 '21 at 13:12