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 (7)

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();
?>

RE: Kodexempel PHP - Added by sunh 6 days ago

Hello. I am a passerby. When have you been interested in your 먹튀가디언 I see an article about먹튀검증 I am interested and leave a comment. 꽁머니 I started to be interested in토토사이트 about 안전놀이터 from the time. Your article about "is really interesting. I don't know how you can think about how to relate "and 먹튀검증. I would appreciate if you posted a lot of data about 안전놀이터 in the future. 스포츠중계 And I am also the person who is writing about 꽁머니 . Please pay attention to my 먹튀보안관 My site is called "and the address is https://twiddeo.com/ and https://mtboan.com/ . Thank you.

RE: Kodexempel PHP - Added by sunh 6 days ago

토토레프리 What a nice comment! 먹튀검증 Nice to meet you. I live in 먹튀 a different country 먹튀검증업체 from you. Your writing will be https://elephonemobile.com/ of great help to 먹튀사이트 me and to many other people living in 메이저놀이터 our country. I was looking for a post like this, but I finally found it. 메이저놀이터 Thank you so much for 사설토토 letting me read this. 메이저사설토토 This post touched me a lot. Actually, I'm running a website. 사설놀이터 I'd like to write on my site, 안전놀이터 too, referring to your post. I'm still lacking a lot, but I'm working hard! 안전놀이터 If you're interested, 토토사이트 please visit my site. I'd appreciate it if you could read my article and leave your comments. 메이저토토사이트 Thank you again for leaving me good posts, and I will visit often and read other posts. 토토사이트추천 Be careful of COVID-19, 토토사이트 have a good day!

RE: Kodexempel PHP - Added by sunh 6 days ago

안전놀이터검증및연구센터 That's a very good story! 토토사이트분석전문블로그 I read your article very deeply. 먹튀검증의새로운패러다임 Hello, my name is https://kipu.com.ua/ and I run a site similar to yours. https://remarka.kz/ Compared to you, https://subtitles.ee/ the site I run is not enough. 먹튀검증 However, 안전놀이터 I am also working hard 토토사이트 to upload 먹튀사이트검증 more than 2 posts a week. 메이저토토사이트 If you are interested, 메이저안전놀이터 please visit 안전한사설토토사이트 my website. 공식안전놀이터 I would appreciate it 먹튀검증커뮤니티 if you could visit 먹튀검증토토사이트 and read my article 먹튀검증토토 and leave your comments. 사설안전놀이터] It will be of great help to me. Anyway, have a nice day today and thank you for your hard work.It's really cool 검증토토사이트. I'd like to know more information. Just like your 안전한먹튀검증 I have 최고의안전놀이터 Nowadays,토토사이트검증업체 the weather is getting colder every day토토안전놀이터 , so wear warm clothes and watch out for a cold 확실한먹튀검증

(1-7/7)