API Use with PHP

Started by col, May 09, 2019, 12:37:57 PM

Previous topic - Next topic
Hi All...

Not sure if this is the right board to post this in but here goes..

I'm trying to access the API's using a PHP script (only language I have some familiarity with) but struggling to get it to give any responses and the 'Help' documentation only shows examples in Python or cURL neither of which i'm familiar with, so got a bit stuck.

Here is the code i'm using (sensitive info removed), I've used similar code to get API's from other things before, so maybe i'm missing some syntaxing...


$api_key = "keyhere";
$api_secret = "secretehere";

$base_url = "https://<ip>/api";
$api_get_url = "/diagnostics/interface/getInterfaceNames";

$url_ints = $base_url.$api_get_url;

$get_opts = array('http' =>
    array(
        'method'  => 'GET',
        'header'  => 'auth='.$api_key.':'.$api_secret."\r\n" .'Content-Type: application/json'
    )
);

$get_context  = stream_context_create($get_opts);
$get_token = file_get_contents($url_ints, false,  $get_context);

$token_array = json_decode($get_token, true);

print_r ($token_array);


Any help would be appreciated.

Thanks!

Hi there,

Basic auth needs to be properly encoded and advertised, see:

https://www.php.net/manual/de/function.stream-context-create.php#74431


Cheers,
Franco

Ahhh silly me.

Working now, thanks!


It would be great if you could post the working script.

Thanks