Welcome Guest, Not a member yet? Register   Sign In
How to get API result in a variable
#1
Question 

There is an API, where I can put following code in the address bar:
Code:
http://www.pokerlions-pokerserver.de/api?Command=SystemStats&JSON=Yes&Password=xxxxx

This returns following:
Code:
{"Result":"Ok","Logins":7,"FilledSeats":4,"OccupiedTables":1,"Threads":376,"UpSeconds":8989446,"UpTime":"104 days  1 hr  4 mins","LocalTime":"2022-04-01 22:21:31","UTCTime":"2022-04-01 20:21:31","DiskFree":66308354048,"Version":"6.32","License":"Gold"}
How can I get the result in a variable ?

The following code does not work:

PHP Code:
$client = \Config\Services::curlrequest();
        $response $client->request('GET','http://www.pokerlions-pokerserver.de/api',[
            'Command' => 'SystemStats',
            'JSON' => 'Yes',
            'Password' => 'xxxxx'
        ]); 

Has anybody an idea for me?
Reply
#2

I assume you mean using json_encode / json_decode?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#3

(This post was last modified: 04-01-2022, 10:59 PM by kenjis.)

What do you mean by "does not work"?
"does not work" does not work when you ask a question.

It seems your code should work.
What did you get and what did you expect?
Reply
#4

OK, maybe I expressed myself wrong.
Here is the full approach:

To use an API I create a new library:
PHP Code:
<?php

namespace App\Libraries;

class 
PokerMaven
{
    private static $url "http://www.pokerlions-pokerserver.de/api";
    private static $pw "xxxxx";

    public static function poker_api($params)
    {
        $params['Password'] = self::$pw;
        $params['JSON'] = 'Yes';
        $curl curl_init(self::$url);
        curl_setopt($curlCURLOPT_POSTtrue);
        curl_setopt($curlCURLOPT_POSTFIELDShttp_build_query($params));
        curl_setopt($curlCURLOPT_TIMEOUT30);
        curl_setopt($curlCURLOPT_RETURNTRANSFERtrue); 
        curl_setopt($curlCURLOPT_VERBOSEfalse);
        curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
        $response curl_exec($curl);
        if(curl_errno($curl)) $obj = (object) array('Result' => ' Error''Error' => curl_error($curl));
        else if (empty($response)) $obj = (object) array('Result' => 'Error''Error' => 'Connection failed');
        else $obj json_decode($response);
        curl_close($curl);
        return $obj;
    }

In my controller I tried to call the API:
PHP Code:
<?php

namespace App\Controllers;

use 
App\Libraries\PokerMaven;
use 
CodeIgniter\HTTP\Request;

class 
Serverbeta extends BaseController
{
    public function stats()
    {
        $params = array("Command" =>"SystemStats");
        $api PokerMaven::poker_api($params);

        myDump($api);
        $this->data['api'] = $api;
        $this->data['title'] = "Pokerserver - Stats";
        return view("Serverbeta/stats"$this->data);
    }

In this case I only get a blank page.
If I append the Port in the API-url
Code:
private static $url = "http://www.pokerlions-pokerserver.de:8087/api";
the server answers following:
Code:
stdClass Object
(
    [Result] =>  Error
    [Error] => Failed to connect to www.pokerlions-pokerserver.de port 8087: Connection refused
)

But when I use the URI case-by-case I get the correct, expected output:
In the address bar:
Code:
http://www.pokerlions-pokerserver.de/api?Command=SystemStats&JSON=Yes&Password=xxxxx
and as output:
Code:
{"Result":"Ok","Logins":0,"FilledSeats":0,"OccupiedTables":0,"Threads":374,"UpSeconds":9026587,"UpTime":"104 days  11 hrs  23 mins","LocalTime":"2022-04-02 08:40:33","UTCTime":"2022-04-02 06:40:33","DiskFree":66352177152,"Version":"6.32","License":"Gold"}
Because of that I suspected that I might get further with the CURL library and changed my controller like this:
PHP Code:
<?php

namespace App\Controllers;

use 
App\Libraries\PokerMaven;
use 
CodeIgniter\HTTP\Request;

class 
Serverbeta extends BaseController
{
    public function stats()
    {
        $client = \Config\Services::curlrequest();
        $response $client->request('GET','http://www.pokerlions-pokerserver.de/api',[
            'Command' => 'SystemStats',
            'JSON' => 'Yes',
            'Password' => 'xxxxx'
        ]);
        myDump($response);
        $this->data['api'] = $api;
        $this->data['title'] = "Pokerserver - Stats";
        return view("Serverbeta/stats"$this->data);
    }

Now I get following:
Code:
CodeIgniter\HTTP\Response Object
(
    [reason:protected] => OK
    [statusCode:protected] => 200
    [pretend:protected] =>
    [protocolVersion:protected] => 1.1
    [validProtocolVersions:protected] => Array
        (
            [0] => 1.0
            [1] => 1.1
            [2] => 2.0
        )

    [body:protected] =>












    [headers:protected] => Array
        (
            [Cache-control] => CodeIgniter\HTTP\Header Object
                (
                    [name:protected] => Cache-control
                    [value:protected] => Array
                        (
                            [0] => no-store
                            [1] => max-age=0
                            [2] => no-cache
                        )

                )

            [Content-Type] => CodeIgniter\HTTP\Header Object
                (
                    [name:protected] => Content-Type
                    [value:protected] =>  text/html
                )

            [Date] => CodeIgniter\HTTP\Header Object
                (
                    [name:protected] => Date
                    [value:protected] =>  Sat, 02 Apr 2022 06:57:36 GMT
                )

            [Server] => CodeIgniter\HTTP\Header Object
                (
                    [name:protected] => Server
                    [value:protected] =>  Apache/2.4.53 (Unix)
                )

            [Accept-Ranges] => CodeIgniter\HTTP\Header Object
                (
                    [name:protected] => Accept-Ranges
                    [value:protected] =>  bytes
                )

            [Transfer-Encoding] => CodeIgniter\HTTP\Header Object
                (
                    [name:protected] => Transfer-Encoding
                    [value:protected] =>  chunked
                )

        )

    [headerMap:protected] => Array
        (
            [cache-control] => Cache-control
            [content-type] => Content-Type
            [date] => Date
            [server] => Server
            [accept-ranges] => Accept-Ranges
            [transfer-encoding] => Transfer-Encoding
        )

    [CSPEnabled:protected] =>
    [CSP] => CodeIgniter\HTTP\ContentSecurityPolicy Object
        (
            [baseURI:protected] =>
            [childSrc:protected] => self
            [connectSrc:protected] => self
            [defaultSrc:protected] =>
            [fontSrc:protected] =>
            [formAction:protected] => self
            [frameAncestors:protected] =>
            [frameSrc:protected] =>
            [imageSrc:protected] => self
            [mediaSrc:protected] =>
            [objectSrc:protected] => self
            [pluginTypes:protected] =>
            [reportURI:protected] =>
            [sandbox:protected] =>
            [scriptSrc:protected] => self
            [styleSrc:protected] => self
            [manifestSrc:protected] =>
            [upgradeInsecureRequests:protected] =>
            [reportOnly:protected] =>
            [validSources:protected] => Array
                (
                    [0] => self
                    [1] => none
                    [2] => unsafe-inline
                    [3] => unsafe-eval
                )

            [nonces:protected] => Array
                (
                )

            [tempHeaders:protected] => Array
                (
                )

            [reportOnlyHeaders:protected] => Array
                (
                )

        )

    [cookieStore:protected] => CodeIgniter\Cookie\CookieStore Object
        (
            [cookies:protected] => Array
                (
                )

        )

    [cookiePrefix:protected] =>
    [cookieDomain:protected] =>
    [cookiePath:protected] => /
    [cookieSecure:protected] =>
    [cookieHTTPOnly:protected] => 1
    [cookieSameSite:protected] => Lax
    [cookies:protected] => Array
        (
        )

    [bodyFormat:protected] => html
)

And this is exactly where I can't get any further...
Can anyone help me with this?

Greetings...
Kigh...
Reply
#5

(This post was last modified: 04-02-2022, 01:08 AM by ignitedcms.)

Does it have Access-Control-Allow-Origin headers restrictions for site domain BTW?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#6

Hmmm...
I don't know...
Where cloud I check this ?
Reply
#7

(This post was last modified: 04-02-2022, 06:58 AM by kenjis.)

http://www.pokerlions-pokerserver.de/api...word=xxxxx

Code:
<html>
<head>

<title>http://www.pokerlions-pokerserver.de/api</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="ROBOTS" content="NOINDEX,FOLLOW">
</head>
<frameset rows="100%,*" border="0">
<frame name="_redirected_content_" src="http://81.169.204.219:8087/api?Command=SystemStats&amp;JSON=Yes&amp;Password=xxxxx" scrolling="auto" frameborder="0">
<noframes>
<body lang=en>
Your browser does not support framesets.
<a href="http://81.169.204.219:8087/api?Command=SystemStats&amp;JSON=Yes&amp;Password=xxxxx">Please click here
to be redirected to the final page.</a>
</body>
</noframes>
</frameset>
</html>

See https://codeigniter4.github.io/userguide...html#query

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index()
    {
        $client = \Config\Services::curlrequest();

        $response $client->request(
            'GET',
            'http://81.169.204.219:8087/api',
            [
                'query' => [
                    'Command'  => 'SystemStats',
                    'JSON'     => 'Yes',
                    'Password' => 'xxxxx',
                ],
            ]
        );

        return $response->getBody();
    }


I got:
Code:
{"Result":"Error","Error":"Password is incorrect"}
Reply
#8

Aaaaah, so it only seems to work if I use the IP address with the port and not the web address as stated by the developer of the API.
I would never have thought of that.
Thank you, NOW I can work with it!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB