loadHTML($html); libxml_clear_errors(); $get_html_value = $dom->getElementById('spanTxHash'); if(empty($get_html_value)){ return _error('当前页面没找到hash'); } $resp['hash'] = $get_html_value->nodeValue; //$get_html_value = '' . $this->contract_address . ''; //$get_html_value = 'BSC-USD'; $get_html_value = ''; if (!(strstr($html, $get_html_value))) { Log::error('合约地址和配置信息不相符:' . $tx_hash); return _error('合约地址和配置信息不相符'); } $xpath = new \DOMXPath($dom); $get_html_value = $xpath->query('//span[@class="badge bg-success bg-opacity-10 border border-success border-opacity-25 text-green-600 fw-medium text-start text-wrap py-1.5 px-2"]'); if ($get_html_value->length > 0 && $get_html_value->item(0)->nodeValue == 'Success') { $resp['pay_status'] = 1; } else { return _error('交易失败'); } $get_html_value = $xpath->query('//span[@class="me-1"]'); if ($get_html_value->length > 0) { $amount = $get_html_value->item(1)->nodeValue;//所有 span class='me-1' 的元素中,第二个就是额度,超过1000会有 逗号分割 $resp['amount'] = str_replace(',', "", $amount); } else { return _error('当前页面没找到交易额度'); } $get_html_value = $dom->getElementById('chunk_decodeori_0_1'); if(empty($get_html_value)){ return _error('当前页面没找到from_address'); } $resp['from_address'] = strtolower($get_html_value->nodeValue);//转小写 $get_html_value = $dom->getElementById('chunk_decodeori_0_2'); if(empty($get_html_value)){ return _error('当前页面没找到to_address'); } $resp['to_address'] = strtolower($get_html_value->nodeValue); return _success($resp); } /** * 根据钱包地址查询交易记录 * * @$address 查询地址 * @$from_to 是查询付款交易(from),还剩收款交易(to),默认from */ public function getTransactionRecordsByAddress(string $from, string $to, int $start_block, int $end_block = 99999999) { if(empty($from) && empty($to)){ return _error('钱包地址不能都为空'); } $from = strtolower($from); $to = strtolower($to); $address = $from; if(empty($from)){ $address =$to; } // 读取远程页面地址 $url = "https://api.bscscan.com/api?module=account&action=tokentx&contractaddress=" . $this->contract_address; $url .= "&page=1&offset=10000&endblock=" . $end_block ."&apikey=" . $this->bsc_api_key; $url .= "&startblock=" . $start_block . "&address=" . $address; Log::log($url); $body = Http::get($url); if (empty($body)) { Log::log('api返回内容为空'); return _error('api返回内容为空'); } // 转成数组 $rsArr = json_decode($body, true); if (empty($rsArr) || !is_array($rsArr)) { Log::log('api返回数据异常,json decode失败'); return _error('api返回数据异常'); } if ($rsArr['status'] != '1') { if($rsArr['message'] == 'No transactions found'){ Log::log('无交易记录:' . $rsArr['message']); return _success(); } Log::log('api返回status不为1,错误信息:' . $rsArr['message']); return _error('api返回status不为1,错误信息:' . $rsArr['message']); } return _success($success_arr); } /** * 获取哈希地址成功状态 * @param $orderInfo * @return array|string */ public function getHashStatus($tx_hash):array { if (empty($tx_hash)) { return _error('hash值不能为空'); } $url = "https://api.bscscan.com/api?module=transaction&action=gettxreceiptstatus"; $url .= "&apikey=" . $this->bsc_api_key; $url .= "&txhash=" . $tx_hash; $body = Http::get($url); if (empty($body)) { return _error('状态api返回内容为空'); } // 转成数组 $rsArr = json_decode($body, true); if (empty($rsArr) || !is_array($rsArr)) { return _error('状态api返回数据异常,json转换失败'); } if ($rsArr['status'] != '1') { return _error('状态api返回status不为1,当前值为:' . $rsArr['status']); } if ($rsArr['result']['status'] != 1) { return _error('状态api返回result中的status不为1,当前值为:' . $rsArr['result']['status']); } return _success(); } /** * 根据时间戳获取最近的区块高度 * api接口返回数据格式 * @param $orderInfo * @return array|string */ public function getBlockNoByTime($time):array { if(!Cache::has('block_' . $time)){ if (empty($time)) { return _error('时间戳不能为空'); } //https://api.bscscan.com/api?module=block&action=getblocknobytime×tamp=1601510400&closest=before&apikey=YourApiKeyToken $url = "https://api.bscscan.com/api?module=block&action=getblocknobytime&"; $url .= "&closest=before";//closest 值还有个参数 after 控制返回接近时间戳之前还是之后的区块高度 $url .= "&apikey=" . $this->bsc_api_key; $url .= "×tamp=" . $time; dump($url); $body = Http::get($url); if (empty($body)) { return _error('获取区块高度api返回内容为空'); } // 转成数组 $rsArr = json_decode($body, true); if (empty($rsArr) || !is_array($rsArr)) { return _error('获取区块高度api返回数据异常,json转换失败'); } if ($rsArr['status'] != '1') { return _error($rsArr['message'] . ' -- ' . $rsArr['result']); } Cache::set('block_' . $time, _success($rsArr['result']), 3600); } //获取开始预约前的区块高度 return Cache::get('block_' . $time); } public function getBlcokNoByCache($time){ if(!Cache::has('block')){ $get_block = (new BscApi())->getBlockNoByTime($start_time); if($get_block['code'] == 0){ Log::info($get_block['msg'] . date('Y-m-d H:i:s')); dump('获取区块高度有误'); dump($get_block); return; } Cache::set('block', $get_block['data'], 3600); } //获取开始预约前的区块高度 $start_block = Cache::get('block'); } }