1

I have this code:

$json = file_get_contents('http://yiimp.ccminer.org/api/wallet?address=DshDF3zmCX9PUhafTAzxyQidwgdfLYJkBrd');
$obj = json_decode($json);
var_dump($obj);

and the object here is empty, no data available but if I access the URL from browser the result is this:

{"currency": "DCR", "unsold": 0.030825917365192, "balance": 0.02007306, "unpaid": 0.05089898, "paid24h": 0.05796425, "total": 0.10886323}

what am I missing?

Sas Gabriel
  • 1,544
  • 2
  • 20
  • 27
  • First of all you can't echo objects – ArtOsi Aug 31 '17 at 08:27
  • when I put that URL in the browser I got a 404. That could be it. In PHP you can check the response from the server by looking at the value of `$http_response_header` immediately after the request completes. Secondly, if you run json_decode, it turns the JSON text into a PHP object, which will generally not echo properly. So you'd have to `var_dump($obj);` to see it on screen. Or just `echo $json;` of course. – ADyson Aug 31 '17 at 08:28
  • @ADyson, sometimes the url just doesn't work for specific people, for me it still works, but it also didn't work a half an hour ago, try again in some time – Sas Gabriel Aug 31 '17 at 08:30
  • that's not very good. Sounds like the server has a problem. But anyway you can do what I suggested in order to check the response within your PHP code. – ADyson Aug 31 '17 at 08:31
  • the value of obj is just empty even if i change to var_dump – Sas Gabriel Aug 31 '17 at 08:33
  • do not use file_get_contents. Please, alltogether STOP. CURL it! – Unamata Sanatarai Aug 31 '17 at 08:33
  • So maybe that's the problem, that the link as you say doesn't work sometimes. – ArtOsi Aug 31 '17 at 08:34
  • do a `var_dump($http_response_header);` just after the call to file_get_contents to verify whether you got a successful response or not. A 200 OK response code would indicate that you ought to get data back. A 404 or other error code would suggest that you won't, for that particular request. Your actual code to output the data is fine, so the issue is more likely to be the flakiness of the API, but it's worth checking using this. – ADyson Aug 31 '17 at 08:38

2 Answers2

2

If you need to go with file_get_contents you need to set the context for the request. Apparently this URL REQUIRES to see a user-agent string in headers (cause, you know... anti-bot-secruity).

The following works:

<?php
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: foo\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://yiimp.ccminer.org/api/wallet?address=DshDF3zmCX9PUhafTAzxyQidwgdfLYJkBrd', false, $context);

var_dump($file);
// string(137) "{"currency": "DCR", "unsold": 0.030825917365192, "balance": 0.02007306, "unpaid": 0.05089898, "paid24h": 0.05796425, "total": 0.10886323}"

However. I Strongly suggest cURL

file_get_contents() is a simple screwdriver. Great for simple GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter. https://stackoverflow.com/a/11064995/2119863

So please stop file_get_contents.

<?php
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://yiimp.ccminer.org/api/wallet?address=DshDF3zmCX9PUhafTAzxyQidwgdfLYJkBrd',
    CURLOPT_USERAGENT => 'Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
var_dump(json_decode($resp));

and you get:

object(stdClass)#1 (6) {
  ["currency"]=>
  string(3) "DCR"
  ["unsold"]=>
  float(0.030825917365192)
  ["balance"]=>
  float(0.02007306)
  ["unpaid"]=>
  float(0.05089898)
  ["paid24h"]=>
  float(0.05796425)
  ["total"]=>
  float(0.10886323)
}
Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
-1

As mentioned by others, there seems to be an issue with the API. Personally, the URL returned data on the first load but could not be reached on my next requests.

This code (with a different URL) works perfectly fine for me:

$json = file_get_contents('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1');
$obj = json_decode($json);
print_r($obj);
DerJacques
  • 322
  • 2
  • 12
  • Yeah code works because issue is not with the code itself.. Also this doesn't look like an answer. – ArtOsi Aug 31 '17 at 08:37
  • if I use another link it also works for me, but i need the specific link to work... this is why i also added the link – Sas Gabriel Aug 31 '17 at 08:37
  • fyi... i don't care if the link doesn't work from time to time, i only need it to work once a day, even if i make more calls during the day – Sas Gabriel Aug 31 '17 at 08:38