Kodexempel PHP

Added by Martin over 4 years ago

<?php

function print_json($j, $prefix='')
{
    foreach($j  as  $key => $value)
    {
        if( is_array($value))
        {
            echo $prefix.$key. PHP_EOL;
            echo print_json($value, $prefix.'  ');
        }
        else
        {
            echo $prefix.$key.':'.$value. PHP_EOL;
        }

    }

}

$username = 'username';
$password = 'password';
$service  = 'NEXTAPI';
$URL='https://api.test.nordnet.se';
$API_VERSION='2';
$public_key_path = "file://./NEXTAPI_TEST_public.pem";

// Prepare base 64 string
$timestamp = intval(round(time() * 1000));
$timestamp = strval($timestamp);
$buf = base64_encode($username) .':' .base64_encode($password).':'.base64_encode($timestamp);

// Encrypt string
$encrypted_hash = null;

if(!openssl_public_encrypt($buf, $encrypted_hash, $public_key_path  ))
{

    echo "can't read public key pem";
    exit();

}

$hash = base64_encode($encrypted_hash);

// Prepare GET request
$ch = curl_init();
$headers[] = "Accept: application/json";

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);

curl_setopt($ch, CURLOPT_URL,  $URL .'/next/'.$API_VERSION. '/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Send GET request
$data = curl_exec($ch);
curl_close($ch);

// Print data
print_json(json_decode ($data, true));

// Prepare POST fields
$post_fields = 'service=' .urlencode($service). '&auth='.urlencode($hash);

// Prepare POST request
$ch = curl_init();

$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,  $URL .'/next/'.$API_VERSION. '/login');

// Send POST request
$data = curl_exec($ch);
curl_close($ch);

// Print data
print_json(json_decode ($data, true));

?>


Replies (4)

RE: Kodexempel PHP - Added by Martin over 4 years ago

Eller om man vill använda Guzzle.

<?php
require('vendor/autoload.php');

use GuzzleHttp\Client;

$username = 'username';
$password = 'password';
$service  = 'NEXTAPI';
$URL='https://api.test.nordnet.se';
$API_VERSION='2';
$public_key_path = "file://./NEXTAPI_TEST_public.pem";

// Prepare base 64 string
$timestamp = intval(round(time() * 1000));
$timestamp = strval($timestamp);
$buf = base64_encode($username) .':' .base64_encode($password).':'.base64_encode($timestamp);

// Encrypt string
$encrypted_hash = null;

if(!openssl_public_encrypt($buf, $encrypted_hash, $public_key_path  )) {
    echo "can't read public key pem";
    exit();

}

$hash = base64_encode($encrypted_hash);

// Prepare GET request
$ch = curl_init();
$headers = ["Accept" => "application/json"];

$client = new GuzzleHttp\Client();

$response = $client->request('POST', $URL .'/next/'.$API_VERSION.'/login',
    ['headers' => $headers,
    'form_params' => [ 'service' => $service, 'auth' => $hash ]
    ]
);

$response = $client->get($URL . '/next/'.$API_VERSION.'/markets', [
    'auth' => [
        $r['session_key'],
        $r['session_key']
    ],
    'headers' => $headers
]);
echo $response->getBody();
?>

RE: Kodexempel PHP - Added by Martin over 4 years ago

Har bytt lösenord nu :)

RE: Kodexempel PHP - Added by Martin over 4 years ago

$r['session_key'] ska vara $response['session_key']

RE: Kodexempel PHP - Added by Martin over 4 years ago

Var lite trött igår så har rensat upp kodexemplet lite idag. Här är ett exempel för att logga in och sedan göra api-anrop i PHP & Guzzle.

<?php
require('vendor/autoload.php');

use GuzzleHttp\Client;

$username = 'username';
$password = 'password';
$service  = 'NEXTAPI';
$URL='https://api.test.nordnet.se';
$API_VERSION='2';
$public_key_path = "file://./NEXTAPI_TEST_public.pem";

// Prepare base 64 string
$timestamp = intval(round(time() * 1000));
$timestamp = strval($timestamp);
$buf = base64_encode($username) .':' .base64_encode($password).':'.base64_encode($timestamp);

// Encrypt string
$encrypted_hash = null;

if(!openssl_public_encrypt($buf, $encrypted_hash, $public_key_path  )) {
    echo "can't read public key pem";
    exit();

}

$hash = base64_encode($encrypted_hash);

// Prepare GET request
$client = new GuzzleHttp\Client();
$headers = ["Accept" => "application/json"];

$response = $client->request('POST', $URL .'/next/'.$API_VERSION.'/login',
    ['headers' => $headers,
    'form_params' => [ 'service' => $service, 'auth' => $hash ]
    ]
);
$r = json_decode($response->getBody(), true);
$response = $client->get($URL . '/next/'.$API_VERSION.'/markets', [
    'auth' => [
        $r['session_key'],
        $r['session_key']
    ],
    'headers' => $headers
]);
echo $response->getBody();
?>

(1-4/4)