0

Setting up a JSON-RPC on my vps which I want to connect via PHP CURL on my website doing a basic request and looking for getmasternodecount.

Tried many scripts and libraries before however none seems to work in my case. Now I try to write some basic php code, but this skill isnt my best.

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

function coinFunction () {

    $feed = 'http://user:pass@ip/';
    $post_string = '{"method": "getmasternodecount", "params": []}';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $feed);
    curl_setopt($ch, CURLOPT_PORT, port);
    curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/stratum', 'Content-length: '.strlen($post_string)));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-length: '.strlen($post_string)));

    $output = curl_exec($ch);
    curl_close($ch);



    return $output;


}

$data = coinFunction();
var_dump($data);

echo $data;

?>

And gives me this data dump: string(127) "HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1 " HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1

When i delete all the var dump information etc, it send me a whitepage and sometimes NULL.

Kindly Regards,

dutchsociety
  • 575
  • 4
  • 22

1 Answers1

1

Let's work with the first snippet. Since it's a POST request, file_get_contents is rather out of place here. Add the following setopt lines:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);

Without those, the result of curl_exec won't contain the returned content.

It would also be advisable to specify the Content-Type of the request (which is application/json). The server might handle it even without, but just in case:

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Content-Type:application/json'));

Authentication is another thing. Credentials in the URL suggest Basic, but the server might expect otherwise... See CURLOPT_HTTPAUTH.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Am I able to chat with you asking some specific questions regarding this topic? – dutchsociety May 23 '20 at 22:04
  • I can't promise realtime answers. What are you stuck on right now? – Seva Alekseyev May 23 '20 at 22:59
  • Guess my approach isnt good here, im using the bitoin RPC json however it isnt working and I dont know why so i tried using other options. But im not that familiar with PHP. I just need one rpc post that request one data variable in the json. – dutchsociety May 23 '20 at 23:47
  • "It isn't working" is not very helpful. What's the value of curl_error()? What's the value of `result`? Also you should check the value of `curl_getinfo($curl, CURLINFO_HTTP_CODE)`, that's how you get HTTP errors like 4xx or 5xx. – Seva Alekseyev May 23 '20 at 23:59
  • Okay, that's progress. :) 403 means your username/password is wrong, or the provided credentials have no access to the desired resource.. – Seva Alekseyev May 24 '20 at 00:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214506/discussion-between-dutchsociety-and-seva-alekseyev). – dutchsociety May 24 '20 at 00:33
  • Thank again for helping me though!! – dutchsociety May 24 '20 at 01:44