Json-RPC, PHP curl, and Bitcoin getblocktemplate request - php

Thanks for stopping in :)
I break things, fiddle around, and then try to put them back together.
It is how I seem to learn best.
(One of) My recent obsession(s) has been with crypto-currency.
Since I have a little bit of knowledge working with API's through PHP and cUrl, I decided to use PHP for my fiddling.
So, I tell myself, "Why not register with a popular pool and see if I can't examine what a piece of 'work' looks like. Simple!"
I'm well aware that PHP, Json-RPC and CPU Bitcoin mining are extremely inefficient. I am not attempting to really create anything that will see the light of day; simply fiddling for fiddling's sake.
Evidently, I am just a bit ignorant on how Json-RPC and Stratum servers operate. I had thought to post this to the Bitcoin specific exchange, however I have the feeling my mistake is much more elementary than that.
The Goal: Get some work and print it to the page so I can inspect it and hopefully learn something.
My Attempt:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
function tickleElmo () {
$feed = 'http://stratum.btcguild.com';
$post_string = '{"method": "getblocktemplate", "params": [], "id": "0"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_PORT, 3333);
curl_setopt($ch, CURLOPT_USERPWD, "elmoworker_1:123");
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 = tickleElmo();
var_dump($data);
?>
So, I had assumed (obviously incorrectly) that simply making a POST to the end-point would yield at least some response. I can ping the server stratum.btcguild.com just fine. Yet, via cUrl I don't get anything at all... the script simply times out and returns false.
I have tried a few different Content-type's, adding a newline character to the end of the post, the getwork and getblocktemplate methods inside the $post_string, and about 2 dozen other hack and slash alterations :(
I believe that the problem lies in my understanding of Json-RPC, as I have only previously worked with simple get API end-points in the past, but I have done my personal best to read up and find a simple explanation and have hit a road-block. I just don't have the tools to understand... :(
Any thoughts or additional sources of research/information from the intelligent folks here #stackoverflow would be greatly appreciated,
Samantha.

Related

php curl -X GET with request body

Here is my problem. I'm running a service on a remote machine working perfectly. The way to get the results from the machine is via api.
curl -X GET http://ip:777/api \
-d "r=request"
It works perfectly on the terminal. Moreover, it works perfectly, if the request query is short. But, it turns into a huge problem once, it passes some length(1800-2000 characters and I need 7k-8k chars).
However, I can't "transliterate" the curl code into PHP. If there is anyone with any idea how to do it please show me the way. As much as, I'm aware, this is a curl GET method with REQUEST BODY.
$long_query = "r=" . $request;
// set the api
curl_setopt($ch, CURLOPT_URL, 'http://ip:777/api');
// i want to get the return
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 2min+ timeout as to make sure that I get a result
curl_setopt($ch, CURLOPT_TIMEOUT, 140);
// Set request method to GET by 0'ing the POST method
curl_setopt($ch, CURLOPT_POST, 0);
// Set query data here with CURLOPT_POSTFIELDS
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($long_query));
$content = curl_exec($ch);
curl_close($ch);
echo $content;
What am I doing wrong in here? If someone knows, please explain as if you are teaching a year old. Thanks in advance!
I think the following doc would help you understand how GET method works. This is from RFC 7231
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
For more details, please refer to this answer.
Alright, here we go with the proper answer.
on terminal,
curl -X GET http://ip:777/api \
-d "r=request"
works perfectly. However, the problem with converting that to php curl is quite troublesome while very easy at the same time.
I've read through every stack problem regarding this and no-one has provided a clear answer to the problem. I'm not sure the reason behind it but as a generous person I'll give out the code so that anyone in the future facing this rare problem will solve it out easily.
Long story short,
curl -X GET -d is the same as curl -X POST -H "X-HTTP-Method-Override: GET".
The actual request is POST but THE SERVER will consider it as a GET. This way you won't face the LONG URI problem.
$long_query = "r=" . $request;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"ip:777/api");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $long_query); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'X-HTTP-Method-Override: GET',
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_setopt($ch, CURLOPT_TIMEOUT, 140);
curl_close ($ch);
var_dump($server_output);
I've set the timeout to 140 as the query is long and it takes a bit of time for the server to go through it and respond (in my case its a json). Nevertheless, I've added var_dump so that anyone who uses it in the future might see if its a serialized array or whatever.
Good luck!

Oanda API v20 requests failing

I have tried the following code and receive a message saying my particular request is not supported, I cannot find any solutions that are not python for the v20 api. any help would be appreciated on what to use or where im going wrong
My error message is:
{"errorMessage":"Requested HTTP method is not supported for supplied
endpoint."}
<?php
$ch = curl_init();
$vars = "price=B&granularity=M5&count=20";
curl_setopt($ch, CURLOPT_URL,"https://api-fxpractice.oanda.com/v3/instruments/EUR_USD/candles");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Content-Type: application/json',
'Authorization: Bearer access-token',
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
?>
My goal here is to successfully connect and get the response from the endpoint containing data. I have no python experience which is why im trying curl or even a javascript solution would work. Am i just not understanding this API at all?
Two things that stand out in the code are:
that there is no actual authorisation code. I don’t know if you have removed it for security while posting, or if you don’t realise you need to create a practice account and get an authorisation code for it? Both are free.
Your code curl_setopt($ch, CURLOPT_POST, 1); suggests you are trying a POST, whereas a GET is required for the \instruments endpoint.
But the simplest thing I can suggest that might help is to look at the following Github bash script that has taken OANDA's V1 API bash cURL example, and updated it for the v2 rest API.
https://github.com/p-burke/oanda-REST-v2-API-bash-script

Setting up proxy for an axis ip camera in PHP

I have been searching for a way to proxy a mjpeg stream from the AXIS M1114 Network Camera.
using the following url setup
http://host:port/axis-cgi/mjpg/video.cgi?resolution=320x240&camera=1
i try to capture the output and make them available to users with a php script running an apache server on ubuntu.
having browsed the web looking for an answer to no avail i come to you.
my ultimate goal is to have users able to link to the proxy like this:
<img src='proxy.php'>
and have the details of all the things in proxy.php.
I have tried using the way of cURL (advised in similar thread here) but i can't get it to work, probably due to lack of knowledge on the inner workings.
currently my very simple proxy.php looks like this
<?php
$camurl = "http://ip:port";
$campath = "axis-cgi/mjpg/video.cgi";
$userpass = "user:pw";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $camurl + $campath);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'resolution=320x240&camera=1');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $userpass);
$result = curl_exec($ch);
header('Content-type: image/jpeg');
echo $result;
curl_close($ch);
?>
My understanding is that this would produce an acceptable output for my plan. But alas.
My question would be if there is a blatant error i do not see. Any simpler option/way of getting the result i aim for is welcome too.
Please point me in the right direction. I happily provide any relevant information i might have missed to provide. Thank you in advance.
solved edit:
After commenting out:
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
changing
curl_setopt($ch, CURLOPT_URL, $camurl + $campath);
to
curl_setopt($ch, CURLOPT_URL, $camurl . $campath); (mixing up some languages)
and most importantly removing a space in the .php file so that the header is actually the header it sort of does what i wanted.
Adding a
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
seems to be needed to get the image displayed as image and not as raw data.

Can't get this content spinning API to work with PHP

Can you help me get this content spinning API working? It was wrote to work with C# but is there a way I can get it to work using PHP? I've been trying to post to the URL stated on that page using cURL, but all I'm getting back is a blank page. Here's the code I'm using:
$url = "http://api.spinnerchief.com/apikey=YourAPIKey&username=YourUsername&password=YourPassword";
// Some content to POST
$post_fields = "SpinnerChief is totally free. Not a 'lite' or 'trial' version, but a 100% full version and totally free. SpinnerChief may be free, but it is also one of the most powerful content creation software tools available. It's huge user-defined thesaurus means that its sysnonyms are words that YOU would normally use in real life, not some stuffy dictionary definition. And there's more...SpinnerChief can only get better, because we listen to our users, We take onboard your ideas and suggestions, and we update SpinnerChief so that it becomes the software YOU want!";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, 9001);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
Can anyone see something wrong I'm doing? Thanks a lot for the help.
The value for CURLOPT_POST should be 1, and the posted data should be set with CURLOPT_POSTFIELDS.

cURL Authentication Problems Datatel R25 XML Feed

I am still fairly new at playing with PHP. I wanted to poke at a small project. I have the ability to access an xml feed from our calendar system at work. The feed requires authentication with a username and password. I am trying to automate pulling this feed. My hope is to one day use it to automate the scheduling of another system but for now I'm just trying to get this side working.
I have read through some of the cURL documentation on php.net. I have also searched through stackoverflow for a few examples. They were helpful in getting me started but not much further. Below is what I have so far. But all I get back is:
UNAUTHORIZED The request requires
authorization
So I'm racking my head to figure out what might be the issue. Part of my problem I'm sure is my ignorance with the actual process behind what is going on between the client and server. When I interact with it normally I just go to the feed URL, the browser prompts me for a username and password, and then after putting that in it gives me an xml page.
I've checked for most of the formatting errors and syntax errors I knew to look for. That may still be it but at this point I think it's more my lack of understanding how it all fits together. Thanks for any help.
Similar issues:
Parsing XML data with Namespaces in PHP
CURL HTTP Authentication at server side
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://somesight.someplace.edu/r25ws/servlet/wrd/run/events.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
?>
I figured it out on my own after looking at a similar tutorial for Twitter. I tried a different approach to formatting the options and it worked fine. I also cut down on the number of options I was sending because it seemed like overkill. Code that works below.
<?php
$username = 'myusername';
$password = 'mypassword';
$datecall = curl_init("http://somesite.someplace.edu/r25ws/servlet/wrd/run/events.xml");
curl_setopt($datecall, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($datecall, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($datecall);
echo $result;
?>

Categories