Basic PHP Connect

Gurumanickam
Gurumanickam edited June 2021 in PHP client
I am new to this forum.

I have IIS/PHP 7.4 in my system. I have installed Composer as indicated in the GitHub.

I have created a folder in inetpub/wwwroot named 'kite' on my laptop.
I copied KiteConnect.php from Github.

I wrote a small prog like this.
<?php
include "KiteConnect.php";

//Got the apikey and apisecrete from my application.
$apikey = "xxxxxxxxxxx";
$apisecret="yyyyyyyyyyyyyyyyy";

//Get Request token from the following URL
//https://kite.zerodha.com/connect/login?api_key=xxxxxxxxxxx
$requesttoken="XXXXXX";

try {
// Initialise.
$kite = new KiteConnect($apikey);
return;
// the program exits with this 'KiteConnect($apikey)' statement. It won't go further.

//$user = $kite->generateSession($requesttoken, $apisecret);

}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
?>
Where I am making the mistake?

Please guide me.
  • rakeshr
    Is the above code throwing an error? If yes, can you paste the whole error stack trace?
    You can go through the PHP example here.
  • Gurumanickam


    This is the error message I am getting.

    From the example you have referred.. I haven't used the
    require_once __DIR__ . '/vendor/autoload.php'; bit.

    Is this the main problem?

    I am not sure about it. What is the autoload.php? What it does? How do I download and use it?

    Thanks.
  • Gurumanickam
    I have already installed Composer in my laptop.
  • Gurumanickam
    Gurumanickam edited June 2021
    Thanks for the lead. Earlier I haven't properly installed Composer in the same folder where my connect page was.

    After I installed composer in the same folder, I am able to move forward to $kite = new KiteConnect($apikey);

    I obtained my request token from https://kite.trade/connect/login?api_key=xxxxxxxxx

    When I used this requesttoken along with secret key obtained from my APP page the page is not working....


    <?php
    require_once __DIR__ . '/composer/vendor/autoload.php';
    use KiteConnect\KiteConnect;

    //Got the apikey and apisecrete from my application.
    $apikey = "xxxxxxxxxxx";
    $apisecret="yyyyyyyyyyyyyyyyy";

    //Get Request token from the following URL
    //https://kite.trade.com/connect/login?api_key=xxxxxxxxxxx
    $requesttoken="zzzzzzzzzzz";

    try {
    // Initialise.
    $kite = new KiteConnect($apikey);
    echo "KiteConnect successful";
    // the program works upto this nicely.

    $user = $kite->generateSession($requesttoken, $apisecret);
    echo "Authentication successful. \n";
    //This line is not working....... Session is not generated. (Please help me)
    }
    catch(Exception $e) {
    echo 'Message: ' .$e->getMessage();
    }
    ?>
  • Gurumanickam
    Still unable to get the Session generated
  • Gurumanickam
    <?php
    require_once __DIR__ . '/vendor/autoload.php';
    use KiteConnect\KiteConnect;

    $api_key = "8rdzulnifq8kd99q";
    $api_secret="fkmiwzvddvo6487s0rovpas1haafyik3";
    $request_token="Fit2yBqFsUaoDQlJYL6JGXE3a8Gmw2zC";

    try {
    $kite = new KiteConnect($api_key);
    echo "KiteConnect OK";
    $user = $kite->generateSession($request_token, $api_secret);

    echo "Authentication successful. \n";

    print_r($user);
    return;
    }
    //catch exception
    catch(Exception $e) {
    echo 'Message: ' .$e->getMessage();
    }

    ?>


    The above is the whole code. "KiteConnect OK" is echoing. But generateSession is not working.

    Please help.
  • rakeshr
    But generateSession is not working.
    Is it throwing any error? Can you paste the error stack trace here?
    Also, if you want to use the current PHP client, please upgrade to PHP >=8.
  • Gurumanickam
    Same error message.

    If the $user = $kite->generateSession($request_token, $api_secret); is removed, the page is working.

    Thanks.
  • Gurumanickam
    Gurumanickam edited June 2021
    I installed PHP 8.0.7 as you suggested. Now I am getting some error message. (Still Session not generating)

    Message: Connection refused for URI https://api.kite.trade/session/token
  • Gurumanickam
    Can anyone help me in this regard?

    Currently I am using PHP 8.0.7 and Composer/KiteConnect v4.

    Suppose, if I need to go to v3, I understand that PHP >7.3. If so, should I uninstall composer and reinstall with v3? Or just copying v3-KiteConnect.php is enough?
  • rakeshr
    Message: Connection refused for URI https://api.kite.trade/session/token
    This is not related to the Kiteconnect session but rather the Redirect URL. Have you set redirect URL on the developer console, as some localhost?
  • phpdevelopment
    I have the same query for my PHP development project. Can anyone get a solution?
  • Luckwin
    //try this..

    try {
    $user = $kite->generateSession($request_token, $api_secret);
    $kite->setAccessToken($user->access_token);
    } catch(Exception $e) {
    echo "Authentication failed: ".$e->getMessage();
    throw $e;
    }

    //maybe this try catch block will give you an error.
Sign In or Register to comment.