0

Here are the codes:

<?php

class General{ 
    #to verify user password with api
    public static function verifyUser($user,$pswd){

        #url
        $url = 'https://api.hp.upm.edu.my/cron/request/verify.php';

        # initiate cur
        $ch = curl_init($url);

        # create json data
        $jsonData = array(
            'jtoken' =>  "test123", 
            'id' => "$user",
            'pass'=> "$pswd"
            );
        #Encode the array into JSON.
        $jsonDataEncoded = json_encode($jsonData);

        #Tell cURL that we want to send a POST request.
        curl_setopt($ch, CURLOPT_POST, 1);
        
        #Attach our encoded JSON string to the POST fields.
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
        
        #Set the content type to application/json
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
        #Execute the request
        $result = curl_exec($ch);
        
        curl_close($ch);
        
        return $result;

    }
}

?>

The passed data are username and password, identified by $user and $pswd respectively

The problem is, the end result should return something.

But I found it empty, after checking.

So, what I learned so far are:

  1. I checked the user and password, they are all intact

  2. when I print $ch during "initiate cur", it prints something

when I printed $ch

  1. However, when I printed $result after curl_exec, it is empty.

What do I do? Where is my mistake? Is it the problem of the api url or soemthing else? Please, I need your help

Edit:

  1. Someone suggested that using json_decode will help with this. But, I have already implemented json_decode, after $result is returned.
 $result = General::verifyUser($_POST['user'], $_POST['pass']);
            $obj = json_decode($result, true);

Above is the method call that passes the username and password to the "General" class.

  1. I have printed curl_error for $ch and it says:

"Could not resolve host: api.hp.upm.edu.my"

  • does this help ? https://stackoverflow.com/questions/17016506/how-to-parse-json-and-access-results – Amin S Jan 19 '23 at 17:13
  • Check [the response code](https://stackoverflow.com/questions/11797680/getting-http-code-in-php-using-curl). We can't know if the request was sent correctly or what happens on that end since we don't know what the server expects or handles different cases. _Side note:_ Dumping `$ch` will always give you that output since `$ch` is just the cURL handler. – M. Eriksson Jan 19 '23 at 17:17
  • @AminS - Before they can decode the response, they need to get something back, which seems to be what they are asking about. – M. Eriksson Jan 19 '23 at 17:20
  • @AminS Thanks for the suggested link, I've already checked it out. However, that does not seem to be helping because json_decode is used after method call to the "General" class as in the code block. The $result will be returned to the method caller and decoded immediately after. – Mesmerised95 Jan 20 '23 at 01:00
  • _"Could not resolve host: api.hp.upm.edu.my"_ - so your server is not able to resolve that host name to an IP address. And neither is my browser. Is this supposed to be a publicly reachable URL? – CBroe Jan 20 '23 at 10:35

0 Answers0