Home > database >  Displaying an API via curl onto a php webpage - Just cant figure it out
Displaying an API via curl onto a php webpage - Just cant figure it out

Time:04-18

So I am trying to display info from a website onto my website via an API using curl.

And I am not to clued up on curl at all :(

So this is what I have to from the source website :

curl -H "IHOSTMN-API-KEY:YourIhostmnApiKeyHere" -X GET "https://ihostmn.com/api/v1/sharing/user/list_all_deposits"

And it should give this response:

{
    "result": {
        "deposits": [
            {
                "deposit_id": 36,
                "share_id": 2,
                "current_amount": 53.077553,
                "total_deposited": 24.91567,
                "total_earned": 28.146885,
                "withdraw_address": "GcUgnY5fFwTv9ef8rsggdxbdQzLEUxpp4c",
                "deposit_address": "GfUgnY5sFwTf9ez8rovtdxdEQzLcbxpb4c"
            },
            {
                "deposit_id": 37,
                "share_id": 5,
                "current_amount": 885.9591,
                "total_deposited": 521.92,
                "total_earned": 472.30566,
                "withdraw_address": "gHWHPs21H8UsSNcbfWxvn5XssAxFkcuZYe",
                "deposit_address": "g4sbWWtD3tf16Dsd8wiaJkar3zhJ82qNKP"
            },
            {
                "deposit_id": 38,
                "share_id": 6,
                "current_amount": 754.5115,
                "total_deposited": 548.52997,
                "total_earned": 416.25214,
                "withdraw_address": "LLqWFFJkNSog6VwsbPWEWE4KcXJrzB6t1K",
                "deposit_address": "LW5Vbt1gEkvVQzfVcpLmidLPpcED1Yp3yu"
            }
        ]
    },
    "error": ""
}

I have created my php webpage but can't make it work, this is what wrote:

curl -H "IHOSTMN-API-KEY:xxxxxxxxxxxxxxxxxxxxxxxxx" -X GET "https://ihostmn.com/api/v1/sharing/user/list_all_deposits"

$obj = json_decode($result);
echo $obj[0]->deposits;

Hopefully one of you will be able to point me in the correct direction, and thank you for taking the time to help if you can.

Mark

CodePudding user response:

Try something like this

<?
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://ihostmn.com/api/v1/sharing/user/list_all_deposits');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');


$headers = array();
$headers[] = 'Ihostmn-Api-Key: YourIhostmnApiKeyHere';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
$obj = json_decode($result);

print_r($obj->result->deposits);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

you use [] for arrays and arrows for objects.

I cannot test the API but this should give you a headstart.

Helpful links: cURL

CodePudding user response:

That worked great thanks, one more quick question if for example I just wanted to display total earned from deposit id 36 how would I do that ?If you could answer that I would be able to reverse engineer the answer to pick out any specific info I needed. Again many thanks for your time and helpful answer, I have pressed the tick and the up arrow so I guess that is good for you. Mark

  • Related