skip to Main Content

I am trying to read this JSON file, which I checked is valid
https://www.webminepool.com/api/PK_MgGG3Z8joogwr28PmkE9i/wmc_rate/1000

Here is my code:

//normally the rate is changed via PHP
$checkbtc = "https://www.webminepool.com/api/PK_MgGG3Z8joogwr28PmkE9i/wmc_rate/1000";

$json = file_get_contents($checkbtc);
$json = utf8_encode($json);
$json_data = json_decode($json,true);
print('<br>...'.$json_data.'...<br>');
var_dump($json_data);


The file in my browser sends :

{"success":true,"satoshi":"26.936"}

which seems correct

The result when I execute php is :

<br>......<br>

So where am I wrong ???
Thanks

file()
file_get_contents()
with or without utf8_encode
STUCK

2

Answers


  1. Seems like the endpoint is doing some checks to validate your user-agent, you better to use cURL with it.

    $checkbtc = "https://www.webminepool.com/api/PK_MgGG3Z8joogwr28PmkE9i/wmc_rate/1000";
    
    $cu = curl_init($checkbtc);
    curl_setopt($cu, CURLOPT_HTTPHEADER, [
      'content-type' => 'application/json',
      'user-agent' => 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0'
    ]);
    curl_setopt($cu, CURLOPT_RETURNTRANSFER,true);
    $json = curl_exec($cu);
    
    //$json = file_get_contents($checkbtc);
    //$json = utf8_encode($json);
    $json_data = json_decode($json,true);
    //print('<br>...'.$json_data.'...<br>');
    var_dump($json_data);
    
    Login or Signup to reply.
  2. Check that here you are getting valid JSON string.

    $json = file_get_contents($checkbtc);

    Remove this line. No need and is deprecated method as well.

    $json = utf8_encode($json);

    From here tested and works.

    $json = '{"success":true,"satoshi":"26.936"}';
    $json_data = json_decode($json,true);
    echo var_dump($json_data);
    

    returns:

    array(2) {
      ["success"]=>
      bool(true)
      ["satoshi"]=>
      string(6) "26.936"
    }
    

    utf8_encode — This function is deprecated as of PHP 8.2.0.
    Alternative: mb_convert_encoding(), which supports ISO-8859-1 and many other encodings.

    Alternative way to get content from APi point:

    • Use cURL when you need more control over the HTTP request, such as setting headers, handling different HTTP methods, or dealing with more complex scenarios like authentication or proxy servers.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search