NodeClient.php 914 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace TronTool;
  3. use GuzzleHttp\Client;
  4. class NodeClient{
  5. protected $client;
  6. static function mainNet(){
  7. return new self('https://api.trongrid.io');
  8. }
  9. static function testNet(){
  10. return new self('https://api.shasta.trongrid.io');
  11. }
  12. function __construct($uri){
  13. $opts = [
  14. 'base_uri' => $uri,
  15. 'verify' => false
  16. ];
  17. $this->client = new Client($opts);
  18. }
  19. function post($api,$payload=[]){
  20. $opts = [
  21. 'json' => $payload
  22. ];
  23. $rsp = $this->client->post($api,$opts);
  24. return $this->handle($rsp);
  25. }
  26. function get($api,$query=[]){
  27. $opts = [
  28. 'query' => $query
  29. ];
  30. $rsp = $this->client->get($api,$opts);
  31. return $this->handle($rsp);
  32. }
  33. function handle($rsp){
  34. $content = $rsp->getBody();
  35. //echo $content . PHP_EOL;
  36. return json_decode($content);
  37. }
  38. function version(){
  39. return '1.0.0';
  40. }
  41. }