• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解php与ethereum客户端交互php实例

php 搞代码 4年前 (2022-01-22) 14次浏览 已收录 0个评论

本篇文章给大家讲述了php与ethereum客户端交互的相关知识点,对此有需要的朋友可以跟着学习下。

php与ethereum rpc server通信

一、Json RPC

Json RPC就是基于json的远程过程调用,这么解释比较抽象。简单来说,就是post一个json格式的数据调用rpc server中的方法. 而这个json格式是固定的, 总的来说有这么几项:

{  "method": "",  "params": [],  "id": idNumber}
  • method: 方法名

  • params: 参数列表

  • id: 对过程调用的唯一标识号

二、构建一个Json RPC客户端

<?phpclass jsonRPCClient {    /**   * Debug state   *   * @var boolean   */  private $debug;    /**   * The server URL   *   * @var string   */  private $url;  /**   * The request id   *   * @var integer   */  private $id;  /**   * If true, notifications are performed instead of requests   *   * @var boolean   */  private $notification = false;    /**   * Takes the connection parameters   *   * @param string $url   * @param boolean $debug   */  public function __construct($url,$debug = false) {    // server URL    $this->url = $url;    // proxy    empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;    // debug state    empty($debug) ? $this->debug = false : $this->debug = true;    // message id    $this->id = 1;  }    /**   * Sets the notification state of the object. In this state, notifications are performed, instead of requests.   *   * @param boolean $notification   *<p style="color:transparent">。本文来源gao!%daima.com搞$代*!码网1</p><cite>搞代gaodaima码</cite>/  public function setRPCNotification($notification) {    empty($notification) ?              $this->notification = false              :              $this->notification = true;  }    /**   * Performs a jsonRCP request and gets the results as an array   *   * @param string $method   * @param array $params   * @return array   */  public function __call($method,$params) {        // check    if (!is_scalar($method)) {      throw new Exception('Method name has no scalar value');    }        // check    if (is_array($params)) {      // no keys      $params = $params[0];    } else {      throw new Exception('Params must be given as array');    }        // sets notification or request task    if ($this->notification) {      $currentId = NULL;    } else {      $currentId = $this->id;    }        // prepares the request    $request = array(            'method' => $method,            'params' => $params,            'id' => $currentId            );    $request = json_encode($request);    $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";    // performs the HTTP POST    $opts = array ('http' => array (              'method' => 'POST',              'header' => 'Content-type: application/json',              'content' => $request              ));    $context = stream_context_create($opts);    if ($fp = fopen($this->url, 'r', false, $context)) {      $response = '';      while($row = fgets($fp)) {        $response.= trim($row)."\n";      }      $this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";      $response = json_decode($response,true);    } else {      throw new Exception('Unable to connect to '.$this->url);    }        // debug output    if ($this->debug) {      echo nl2br($debug);    }        // final checks and return    if (!$this->notification) {      // check      if ($response['id'] != $currentId) {        throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');      }      if (!is_null($response['error'])) {        throw new Exception('Request error: '. var_export($response['error'], true));      }            return $response['result'];          } else {      return true;    }  }}?>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解php与ethereum客户端交互php实例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址