Author

Topic: Kraken API help (Read 4666 times)

legendary
Activity: 1106
Merit: 1005
July 01, 2014, 03:20:48 PM
#8
I'm a noob with PHP, I have read several PHP tutorials, but none of them go very deep, they're all pretty useless.

can anyone explain me how to actually get a working php script that can read up-to-date market data from kraken?

and/or explain what the above code does exactly?


getting:

Quote
Fatal error: Uncaught exception 'KrakenAPIException' with message 'CURL error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed' in /home/a3340896/public_html/KrakenAPIClient.php:94 Stack trace: #0 /home/a3340896/public_html/kraken.php(9): KrakenAPI->QueryPublic() #1 {main} thrown in /home/a3340896/public_html/KrakenAPIClient.php on line 94

for some reason is has to do with SSL verify, i have to set it to FALSE apperantly, is this a security risk?

Why doesn't it work when i set it to true?

Code:
    function __construct($key, $secret, $url='https://api.kraken.com', $version='0', $sslverify=false)


Like OP:
KrakenAPIClient.php
Code:
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
/**
 * Reference implementation for Kraken's REST API.
 *
 * See https://www.kraken.com/help/api for more info.
 *
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013 Payward, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

class KrakenAPIException extends ErrorException {};

class 
KrakenAPI
{
    protected 
$key;     // API key
    
protected $secret;  // API secret
    
protected $url;     // API base URL
    
protected $version// API version
    
protected $curl;    // curl handle

    /**
     * Constructor for KrakenAPI
     *
     * @param string $key API key
     * @param string $secret API secret
     * @param string $url base URL for Kraken API
     * @param string $version API version
     * @param bool $sslverify enable/disable SSL peer verification.  disable if using beta.api.kraken.com
     */
    
function __construct($key$secret$url='https://api.kraken.com'$version='0'$sslverify=false)
    {
        
$this->key $key;
        
$this->secret $secret;
        
$this->url $url;
        
$this->version $version;
        
$this->curl curl_init();

        
curl_setopt_array($this->curl, array(
            
CURLOPT_SSL_VERIFYPEER => $sslverify,
            
CURLOPT_SSL_VERIFYHOST => 2,
            
CURLOPT_USERAGENT => 'Kraken PHP API Agent',
            
CURLOPT_POST => true,
            
CURLOPT_RETURNTRANSFER => true)
        );
    }

    function 
__destruct()
    {
        
curl_close($this->curl);
    }

    
/**
     * Query public methods
     *
     * @param string $method method name
     * @param array $request request parameters
     * @return array request result on success
     * @throws KrakenAPIException
     */
    
function QueryPublic($method, array $request = array())
    {
        
// build the POST data string
        
$postdata http_build_query($request'''&');

        
// make request
        
curl_setopt($this->curlCURLOPT_URL$this->url '/' $this->version '/public/' $method);
        
curl_setopt($this->curlCURLOPT_POSTFIELDS$postdata);
        
curl_setopt($this->curlCURLOPT_HTTPHEADER, array());
        
$result curl_exec($this->curl);
        if(
$result===false)
            throw new 
KrakenAPIException('CURL error: ' curl_error($this->curl));

        
// decode results
        
$result json_decode($resulttrue);
        if(!
is_array($result))
            throw new 
KrakenAPIException('JSON decode error');

        return 
$result;
    }

    
/**
     * Query private methods
     *
     * @param string $path method path
     * @param array $request request parameters
     * @return array request result on success
     * @throws KrakenAPIException
     */
    
function QueryPrivate($method, array $request = array())
    {
        if(!isset(
$request['nonce'])) {
            
// generate a 64 bit nonce using a timestamp at microsecond resolution
            // string functions are used to avoid problems on 32 bit systems
            
$nonce explode(' 'microtime());
            
$request['nonce'] = $nonce[1] . str_pad(substr($nonce[0], 26), 6'0');
        }

        
// build the POST data string
        
$postdata http_build_query($request'''&');

        
// set API key and sign the message
        
$path '/' $this->version '/private/' $method;
        
$sign hash_hmac('sha512'$path hash('sha256'$request['nonce'] . $postdatatrue), base64_decode($this->secret), true);
        
$headers = array(
            
'API-Key: ' $this->key,
            
'API-Sign: ' base64_encode($sign)
        );

        
// make request
        
curl_setopt($this->curlCURLOPT_URL$this->url $path);
        
curl_setopt($this->curlCURLOPT_POSTFIELDS$postdata);
        
curl_setopt($this->curlCURLOPT_HTTPHEADER$headers);
        
$result curl_exec($this->curl);
        if(
$result===false)
            throw new 
KrakenAPIException('CURL error: ' curl_error($this->curl));

        
// decode results
        
$result json_decode($resulttrue);
        if(!
is_array($result))
            throw new 
KrakenAPIException('JSON decode error');

        return 
$result;
    }
}

Kraken.php

Code:
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

require_once 
'KrakenAPIClient.php';
$kraken = new KrakenAPI('KEY''SECRET'); 

$res $kraken->QueryPublic('Ticker', array('pair' => 'XXBTZEUR'));

echo 
$res["result"]["XXBTZEUR"]["a"][0];

print_r($res);

It works now, but only with SSL disabled... (and i have no clue how it works, i just copy-pasted it basically).
newbie
Activity: 10
Merit: 0
November 19, 2013, 05:59:11 PM
#7
Thanks mate! You are awesome!

It worked quite well, I also formated the number to show only 2 decimal places.

Code:
$valor = $res["result"]["XXBTZEUR"]["a"][0];

$formatted_number = round($valor,2);

echo $formatted_number;

member
Activity: 104
Merit: 10
Pecunia non olet
November 19, 2013, 05:51:45 PM
#6
Fixed my answer in my post above.

You should enable the display of errors in your php code - so you get at least some hints (on what to google) Wink

Code:
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
This should be your first lines after
newbie
Activity: 10
Merit: 0
November 19, 2013, 05:48:04 PM
#5
That would be :

Code:
echo $res[a][0];

That code returns a blank page  Undecided
member
Activity: 104
Merit: 10
Pecunia non olet
November 19, 2013, 05:42:35 PM
#4
That would be :

Code:
echo $res["result"]["XXBTZEUR"]["a"][0];

*EDIT* Sry too fast - fixed
newbie
Activity: 10
Merit: 0
November 19, 2013, 05:36:12 PM
#3
Anyone?
newbie
Activity: 10
Merit: 0
November 19, 2013, 07:52:37 AM
#2
I have figured out most of the code, this is what I have at the moment:

KrakenAPIClient.php

Code:
/**
 * Reference implementation for Kraken's REST API.
 *
 * See https://www.kraken.com/help/api for more info.
 *
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013 Payward, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

class KrakenAPIException extends ErrorException {};

class 
KrakenAPI
{
    protected 
$key;     // API key
    
protected $secret;  // API secret
    
protected $url;     // API base URL
    
protected $version// API version
    
protected $curl;    // curl handle

    /**
     * Constructor for KrakenAPI
     *
     * @param string $key API key
     * @param string $secret API secret
     * @param string $url base URL for Kraken API
     * @param string $version API version
     * @param bool $sslverify enable/disable SSL peer verification.  disable if using beta.api.kraken.com
     */
    
function __construct($key$secret$url='https://api.kraken.com'$version='0'$sslverify=true)
    {
        
$this->key $key;
        
$this->secret $secret;
        
$this->url $url;
        
$this->version $version;
        
$this->curl curl_init();

        
curl_setopt_array($this->curl, array(
            
CURLOPT_SSL_VERIFYPEER => $sslverify,
            
CURLOPT_SSL_VERIFYHOST => 2,
            
CURLOPT_USERAGENT => 'Kraken PHP API Agent',
            
CURLOPT_POST => true,
            
CURLOPT_RETURNTRANSFER => true)
        );
    }

    function 
__destruct()
    {
        
curl_close($this->curl);
    }

    
/**
     * Query public methods
     *
     * @param string $method method name
     * @param array $request request parameters
     * @return array request result on success
     * @throws KrakenAPIException
     */
    
function QueryPublic($method, array $request = array())
    {
        
// build the POST data string
        
$postdata http_build_query($request'''&');

        
// make request
        
curl_setopt($this->curlCURLOPT_URL$this->url '/' $this->version '/public/' $method);
        
curl_setopt($this->curlCURLOPT_POSTFIELDS$postdata);
        
curl_setopt($this->curlCURLOPT_HTTPHEADER, array());
        
$result curl_exec($this->curl);
        if(
$result===false)
            throw new 
KrakenAPIException('CURL error: ' curl_error($this->curl));

        
// decode results
        
$result json_decode($resulttrue);
        if(!
is_array($result))
            throw new 
KrakenAPIException('JSON decode error');

        return 
$result;
    }

    
/**
     * Query private methods
     *
     * @param string $path method path
     * @param array $request request parameters
     * @return array request result on success
     * @throws KrakenAPIException
     */
    
function QueryPrivate($method, array $request = array())
    {
        if(!isset(
$request['nonce'])) {
            
// generate a 64 bit nonce using a timestamp at microsecond resolution
            // string functions are used to avoid problems on 32 bit systems
            
$nonce explode(' 'microtime());
            
$request['nonce'] = $nonce[1] . str_pad(substr($nonce[0], 26), 6'0');
        }

        
// build the POST data string
        
$postdata http_build_query($request'''&');

        
// set API key and sign the message
        
$path '/' $this->version '/private/' $method;
        
$sign hash_hmac('sha512'$path hash('sha256'$request['nonce'] . $postdatatrue), base64_decode($this->secret), true);
        
$headers = array(
            
'API-Key: ' $this->key,
            
'API-Sign: ' base64_encode($sign)
        );

        
// make request
        
curl_setopt($this->curlCURLOPT_URL$this->url $path);
        
curl_setopt($this->curlCURLOPT_POSTFIELDS$postdata);
        
curl_setopt($this->curlCURLOPT_HTTPHEADER$headers);
        
$result curl_exec($this->curl);
        if(
$result===false)
            throw new 
KrakenAPIException('CURL error: ' curl_error($this->curl));

        
// decode results
        
$result json_decode($resulttrue);
        if(!
is_array($result))
            throw new 
KrakenAPIException('JSON decode error');

        return 
$result;
    }
}

And

kraken.php

Code:

require_once 'KrakenAPIClient.php';
$kraken = new KrakenAPI('MY API''MY SECRET'); 

$res $kraken->QueryPublic('Ticker', array('pair' => 'XXBTZEUR'));

print_r($res);

This php returns the following:

Code:
Array ( [error] => Array ( ) [result] => Array ( [XXBTZEUR] => Array ( [a] => Array ( [0] => 420.00000 [1] => 16 ) [b] => Array ( [0] => 390.00000 [1] => 2 ) [c] => Array ( [0] => 390.00000 [1] => 0.14774000 ) [v] => Array ( [0] => 199.99938193 [1] => 362.02487469 ) [p] => Array ( [0] => 476.40104 [1] => 464.88570 ) [t] => Array ( [0] => 403 [1] => 746 ) [l] => Array ( [0] => 370.00000 [1] => 370.00000 ) [h] => Array ( [0] => 551.79970 [1] => 551.79970 ) [o] => 498.00000 ) ) )

And I want to get only the a variable, in this case it would be 420.00000

Please, can anybody tell me how I can do this?
newbie
Activity: 10
Merit: 0
November 18, 2013, 03:19:28 PM
#1
Hello everybody!

This is my first post. I'm from Portugal and i've been into bitcoins for about 6 months.

I'm making a portuguese blog, but I'm stuck with the Kraken API, since programming isn't really my thing.

Does anybody here have a php script that displays only the bid value, in euros?

I will donate some coins to anybody that can help me do this.

Thank you very much.

km92
Jump to: