Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Python to Php - conversione script

    Salve,
    ho un problema che non riesco a superare,
    devo utilizzare delle API di "Coinbase" per gestire varie funzioni,
    ho un esempio ufficiale in Python, vorrei realizzarlo in php, ho fatto un test ma chiaramente non funziona, credo con certezza che ho proprio sbagliato strada.
    Potreste darmi una mano per farlo funzionare ??

    Python ufficiale:
    codice:
    API_KEY = 'API_KEY'
    API_SECRET = 'API_SECRET'
    
    
    # Create custom authentication for Coinbase API
    class CoinbaseWalletAuth(AuthBase):
        def __init__(self, api_key, secret_key):
            self.api_key = api_key
            self.secret_key = secret_key
    
    
        def __call__(self, request):
            timestamp = str(int(time.time()))
            message = timestamp + request.method + request.path_url + (request.body or '')
            signature = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
    
    
            request.headers.update({
                'CB-ACCESS-SIGN': signature,
                'CB-ACCESS-TIMESTAMP': timestamp,
                'CB-ACCESS-KEY': self.api_key,
            })
            return request
    
    
    api_url = 'https://api.coinbase.com/v2/'
    auth = CoinbaseWalletAuth(API_KEY, API_SECRET)
    
    
    # Get current user
    r = requests.get(api_url + 'user', auth=auth)
    print r.json()
    # {u'data': {u'username': None, u'resource': u'user', u'name': u'User'...
    
    
    # Send funds
    tx = {
        'type': 'send',
        'to': 'user@example.com',
        'amount': '10.0',
        'currency': 'USD',
    }
    r = requests.post(api_url + 'accounts/primary/transactions', json=tx, auth=auth)
    print r.json()
    # {u'data': {u'status': u'pending', u'amount': {u'currency': u'BTC'...

    Il mio test in php:
    codice:
    $API_KEY = 'API_KEY';
    $API_SECRET = 'API_SECRET';
    
    
    $timestamp = time();
    $message = $timestamp + 'POST' + 'my-url' + '';
    $signature = hash_hmac('SHA256', $message, $API_SECRET);
    
    
    $headers = array(
    					'CB-ACCESS-SIGN: '.signature,
    					'CB-ACCESS-TIMESTAMP: '.timestamp,
    					'CB-ACCESS-KEY: '.api_key
    				); 
    
    
    $api_url = 'https://api.coinbase.com/v2/';
    
    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_POST, 1);
    $data = curl_exec($ch); 
    
    
    if(curl_errno($ch))
    { 
        echo "Errore: " . curl_error($ch); 
    }
    else
    { 
        var_dump($data); 
        curl_close($ch); 
    }

    Ho scritto codice procedurale senza classe per comodità.
    Quanti errori ho fatto ??

  2. #2
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    Inizia a correggere l’url della prima request, manca “user”
    poi correggi anche il metodo, la prima è GET, tu chiami con POST

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.