从PHP中的Curl JSON请求中检索/访问数据

时间:2015-01-01 17:27:40

标签: php json api curl

我在解决如何从以下API请求中访问响应时遇到问题。虽然公共API很简单并返回我可以扔进对象/数组的JSON,但我不太了解如何使用API​​密钥.etc访问请求的响应。最终,我希望能够以与公共API类似的方式访问项目:

$contents = file_get_contents($url);
$json = json_decode($contents, true); 
//PRINT IT
echo $json['something']['something'];

这是开发API文档中提供的代码:

$apikey = "xxx";
$apisecret = "yyy";

$nonce=time();
$uri='https://bittrex.com/api/v1.1/account/getbalances?apikey='.$apiKey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);

如果我尝试转储$ obj,则返回'1'。

//DEBUGGING
echo '<pre>';print_r($obj);exit;

这就是响应的样子:

{
"success" : true,
"message" : "",
"result" : [{
        "Currency" : "DOGE",
        "Balance" : 0.00000000,
        "Available" : 0.00000000,
        "Pending" : 0.00000000,
        "CryptoAddress" : "DLxcEt3AatMyr2NTatzjsfHNoB9NT62HiF",
        "Requested" : false,
        "Uuid" : null

    }, {
        "Currency" : "BTC",
        "Balance" : 14.21549076,
        "Available" : 14.21549076,
        "Pending" : 0.00000000,
        "CryptoAddress" : "1Mrcdr6715hjda34pdXuLqXcju6qgwHA31",
        "Requested" : false,
        "Uuid" : null
    }
]

}

以下是API文档:https://bittrex.com/Home/Api

此外,上面的代码会自动在页面上打印响应,打印的是哪一行?

我是API的新手,很抱歉,如果这是一个愚蠢的问题,我就是无法理解它。

由于

/////分辨

我添加了以下代码行,但它确实有效。

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);

1 个答案:

答案 0 :(得分:0)

尝试一下......

<?php
$contents ='{
"success" : true,
"message" : "Test Message",
"result" : [{
        "Currency" : "DOGE",
        "Balance" : 0.00000000,
        "Available" : 0.00000000,
        "Pending" : 0.00000000,
        "CryptoAddress" : "DLxcEt3AatMyr2NTatzjsfHNoB9NT62HiF",
        "Requested" : false,
        "Uuid" : null

    }, {
        "Currency" : "BTC",
        "Balance" : 14.21549076,
        "Available" : 14.21549076,
        "Pending" : 0.00000000,
        "CryptoAddress" : "1Mrcdr6715hjda34pdXuLqXcju6qgwHA31",
        "Requested" : false,
        "Uuid" : null
    }
]
}';

$json = json_decode($contents, true); 
echo '<pre>';
print_r($json); 
echo "</pre><br>";

echo "Success is ".$json['success']."<br>";  // Here success is a boolean
echo "Message is ".$json['message']."<br>"; // Here message is a string
echo "In Result  first currency is ".$json['result'][0]['Currency']."<br>"; //Here result is an array...So Indexs should be mentioned... then string name
echo "In Result  second currency is  ".$json['result'][1]['Currency']."<br>"; 
      ?>

输出将是这样的

Array
(
    [success] => 1
    [message] => Test Message
    [result] => Array
        (
            [0] => Array
                (
                    [Currency] => DOGE
                    [Balance] => 0
                    [Available] => 0
                    [Pending] => 0
                    [CryptoAddress] => DLxcEt3AatMyr2NTatzjsfHNoB9NT62HiF
                    [Requested] => 
                    [Uuid] => 
                )

            [1] => Array
                (
                    [Currency] => BTC
                    [Balance] => 14.21549076
                    [Available] => 14.21549076
                    [Pending] => 0
                    [CryptoAddress] => 1Mrcdr6715hjda34pdXuLqXcju6qgwHA31
                    [Requested] => 
                    [Uuid] => 
                )

        )

)

Success is 1
Message is Test Message
In Result first currency is DOGE
In Result second currency is BTC