duancong2160 2015-01-01 17:27
浏览 44

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

I'm having issues figuring out how to access the response from the following API request. Whilst the public API is simple and returns JSON that I can throw into object/array, I'm not quite understanding how to access the response of the request with API keys .etc. Ultimately I would like to be able to access items in a similar way to the public API:

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

This is the code supplied in the development API docs:

$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);

If I try and dump $obj, it returns '1'.

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

This is what the response should look like:

{
"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
    }
]

}

Here's the API documentation: https://bittrex.com/Home/Api

Also, the above code automatically prints the response on the page, what line is printing it all?

I'm quite new to API's so sorry if this is a daft question, I just can't get my head round it.

Thanks

/////RESOLVED

I added the following lines of code and it worked.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
  • 写回答

1条回答 默认 最新

  • douba1617 2015-01-01 17:54
    关注

    Try it out....

    <?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>"; 
          ?>
    

    Output will be like this

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。