0

im new in PHP, and I have a task to get a data from my client using an API.. but the thing is, can't understand this API even after researching and trying about this..

My client has given me details for the API

URL - http://222.126.31.19:8084/api/login

Username - admin123

Password - test123

those are just a dummy values.. my client also said value should be the token response from login api and prepended by the word "Bearer "

After doing a research.. I tried this codes but nothing appears on my browser.. any idea or suggestion that I can do..

function CallAPI($method, $url, $data = false)
    {
        $curl = curl_init();

        switch ($method)
        {
            case "POST":
                curl_setopt($curl, CURLOPT_POST, 1);

                if ($data)
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                break;
            case "PUT":
                curl_setopt($curl, CURLOPT_PUT, 1);
                break;
            default:
                if ($data)
                    $url = sprintf("%s?%s", $url, http_build_query($data));
        }

        // Optional Authentication:
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_USERPWD, "admin123:test123");

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($curl);

        curl_close($curl);

        return $result;
       }

Do i need to put this on a button or something? Sorry If I'm asking here, It's been a day and I can't solve it.

Ganzo Palumi
  • 77
  • 2
  • 12
  • 1
    You at least need to **invoke** your function; you would need to call `CallAPI();` at some point, passing in the parameters. Something like `CallAPI('POST', 'http://222.126.31.19:8084/api/login');`. – Obsidian Age Mar 15 '19 at 01:28
  • since every api is different you really dont have enough information to be able to do anything –  Mar 15 '19 at 01:32
  • I would recommend you to read about Authorization Headers and Bearer Tokens. How to read or return them using PHP? How to generate a Token or JSON Web Token to authenticate users over an API? https://stackoverflow.com/questions/40582161/how-to-properly-use-bearer-tokens https://stackoverflow.com/questions/30426047/correct-way-to-set-bearer-token-with-curl – Ahmad Shahzad Mar 15 '19 at 01:33
  • Did the client give any API documentation? They really should be providing some form of documentation if they want you to be using their API successfully. Does the client definitely not need to whitelist your IP address or anything? – BT643 Mar 15 '19 at 01:33
  • @BT643 , they gave a documentation consisting of the url, username, password and token – Ganzo Palumi Mar 15 '19 at 01:36
  • can i ask, if they should provide the authorization code? – Ganzo Palumi Mar 15 '19 at 01:42

1 Answers1

0

Most REST API's are working with some kind of session-id. If you are logging in, you will get an session-id for further calls and a list of possible calls for your login-credentials. But every API is working different. Like BT643 said, they have to give you some form of documentation. I use the following code to access some API's i am using. Try it out, if you like.

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, 'http://222.126.31.19:8084/api/login');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_USERPWD, 'admin123' . ":" . 'test123');

    $out = json_decode(curl_exec($ch));

    curl_close($ch);

Then you can print out the return-result (if there is any) with:

echo '<pre>' 
var_dump($out);
echo '</pre>';

This is just to look if you get any result from your call.

Larchfomos
  • 11
  • 5