使用PHP查询STEEM区块链 / Using PHP to query the STEEM blockchain
话说,PHP是世界上最好的语言,咳咳。但是这个最好的语言,我大概有7-8年没有去使用了,现在几乎已经不会用啦。之前学习和写的和STEEM区块链相关的程序都是用的Python。今天突然冒出个想法,写个PHP访问STEEM区块链的简单的例子。然后,写的时候才发现,各种不习惯。其实Python我也没没学两天,PHP则正经八百的用过好长时间呢,结果我觉得我完全变成PHP初学者了。好在,这个语言还算好学。于是磕磕绊绊写了一个程序,然后发现各种不优雅,简化简化简化,简化成现在这个样子了。为了演示核心流程,不包含任何错误处理之类的。
PHP is one of the popular programming languages,today, I wrote a simple PHP script to query the STEEM blockchain.
PHP程序 / The Script
闲话少叙,直接上脚本
<?
class Steemd
{
private $ch;
function __construct(){
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, 'https://steemd.steemit.com');
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
}
function __destruct() {
curl_close($this->ch);
}
private function json_rpc_body($method, $params){
$request = array(
"jsonrpc" => "2.0",
"method" => $method,
"params" => $params,
"id" => 0
);
return json_encode($request);
}
public function exec($method, $params = array()) {
$data = $this->json_rpc_body($method, $params);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($this->ch);
$response = json_decode($response, true);
if (array_key_exists('error', $response)) {
var_dump($response['error']);
die();
}
return $response['result'];
}
public function get_account($account){
$result = $this->exec('get_accounts', [[$account]]);
return $result;
}
}
$steemd = new Steemd;
echo('<pre>');
print_r($steemd->exec(get_chain_properties,[]));
print_r($steemd->get_account('oflyhigh'));
echo('</pre>');
//unset($steemd);
?>
简单解释 / Explain
程序分成两部分
- Steemd类
- 测试代码
This script is divided into two parts, Steemd Class and some simple testing codes
Steemd类 / Steemd Class
使用PHP curl 来执行HTTP请求, 使用'https://steemd.steemit.com'作为请求节点。
I use curl to execute HTTP requests, use 'https://steemd.steemit.com' as RPC node.
Steemd Class 提供两个接口,exec以及get_account
There are two methods provided by Steemd Class , exec and get_account.
exec是底层接口,你可以直接调用这个接口来执行steem blockchain API 操作。
exec is low level interface, you can call it directly to perform operations to query steem blockchain.
get_account是对exe的封装,对用户更加友好,它仅仅是一个封装示例,你可以封装更多的功能。
get_account is a user friendly encapsulation for exe, it just a example, you can encapsulate more functions yourself.
测试代码 / Testing codes
就是简单测试一下而已,没啥可以说的
用print_r让结果看起来更好看
Some simple testing codes to test Steemd class and two interfaces exec and get_account
Use print_r to make the results look better.
运行结果 / Results
脚本仅供参考,使用风险自负!
This script is for reference only, use it at your own risk.
Thanks Alot Bro I Always Wanted To Know How To Use The Steemit Api With PHP And It Appears I Have To Learn Json-RPC :)
哈哈其實怎麼人人都說PHP是最好的語言? 我只懂得很基本的PHP...
哈哈哈,这是网络上的一个梗
哈哈,查到了,所以基本上把PHP換上任何一種其他語言也可以的
不不,PHP才是世界上最好的语言,不服来辩 😄
C才是世界上最好的語言!! 😠😠
php里用curl调用网络资源查询最好加上timeout,要不经常会100% cpu占用。
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
谢谢
只是为了演示最基本的功能 😄
离实用十万八千里呢
意思是说以后你要返回世界上最好语言的大本营了? 嘿嘿
我只是用来测试一下最最最最基本的访问流程 😄
O哥,確認一下,文章裡提到的exe是不是就是exec. 多謝。
哈哈,笔误笔误
已经修改过来
谢谢指正
不敢,多谢O哥传授秘笈
Upvoted Resteemed and following all in this thread
Hi @frankbuse,
Check out free courses on my blog! Many various courses 100% discount!
First part here!
https://steemit.com/education/@arathron/free-udemy-online-courses-1
Thanks for this how to, i wanted to give it a try for the SEEMPI project im working on and i have installed Curl and php and saved your code in index.php but when loading the file it displays code instead of the output:
The code that on page after loading index.php is starting with:
ch = curl_init(); curl_setopt($this->ch, CURLOPT_URL, 'https://steemd.steemit.com');..............
........ending with:
');
print_r($steemd->exec(get_chain_properties,[]));
print_r($steemd->get_account('oflyhigh'));
echo('
'); //unset($steemd); ?>
Any suggestions, what am i doing wrong ?
1.php
2.php
Try above two example php files
If 2.php works well, then change my script as it. (Add php after <? )
If both of them don't work, you need to check your HTTP server setting, add php support to it.
PHP忘干净鸟😭
我居然把世界上最好的语言忘干净鸟😭
😭😭😭😭😭😭😭😭😭😭😭
php 全宇宙第一☝️
Hi @jubi,
Check out free courses on my blog! Many various courses 100% discount!
Please follow me to be updated!
First part here!
https://steemit.com/education/@arathron/free-udemy-online-courses-1
Hi @oflyhigh,
Check out free courses on my blog! Many various courses 100% discount!
Please follow me to be updated!
First part here!
https://steemit.com/education/@arathron/free-udemy-online-courses-1
O哥好厉害!
😳
Hi @sylvia1997,
Check out many various courses on my blog! 100% discount! Remember - FIRST COME FIRST SERVED!
Please follow me to be updated!
First part here!
https://steemit.com/education/@arathron/free-udemy-online-courses-1
终于不用python了 php可以看懂一点。
o哥 你会的语言可真多
我以前会N种语言,现在都忘干净了
我会说汉语,能看懂一点英语,哈哈
厉害!