Commit fa6be74b authored by rlgy's avatar rlgy

爬取交易状态

parent 7245a9c9
......@@ -14,6 +14,7 @@ use api\base\BaseController;
use common\models\pwallet\Coin;
use common\business\CoinBusiness;
use common\business\ExchangeBusiness;
use common\business\BrowerBusiness;
/**
* 币种信息管理控制器
......@@ -71,8 +72,8 @@ class CoinController extends BaseController
throw new Exception('8', '币种不存在');
}
$result = (array)$miner_fee->getAttributes();
$result['min']=(float)$result['min'];
$result['max']=(float)$result['max'];
$result['min'] = (float)$result['min'];
$result['max'] = (float)$result['max'];
return $result;
}
......@@ -101,4 +102,20 @@ class CoinController extends BaseController
return ExchangeBusiness::SearchByName($page, $limit, $condition);
}
}
/**
* 返回交易状态
*
* @return mixed
*/
public function actionTransaction()
{
$request = Yii::$app->request;
$name = $request->post('name', '');
$txhash = $request->post('txhash', '');
if ($name && $txhash) {
return BrowerBusiness::getTransStatus($name, $txhash);
}
return false;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-7-13
* Time: 上午10:21
*/
namespace common\business;
use common\service\brower\BrowerFactory;
class BrowerBusiness
{
/**
* 获取交易信息
*
* @param string $name 区块链浏览器
* @param string $txhash 交易hash
* @return mixed|string|boolean
*/
public static function getTransStatus(string $name, string $txhash)
{
$status = BrowerFactory::getInstance($name)->getStatus($txhash);
if ($status) {
return ['code' => 0, 'status' => $status];
} else {
return [];
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-7-13
* Time: 上午10:01
*/
namespace common\service\brower;
class Brower implements BrowerInterface
{
public function getStatus(string $txhash)
{
return false;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-7-13
* Time: 上午9:56
*/
namespace common\service\brower;
class BrowerFactory
{
public static $instances;
/**
* @param $name
* @return BrowerInterface
*/
public static function getInstance($name)
{
$name = strtolower($name);
if (!isset(self::$instances[$name]) || !(self::$instances[$name] instanceof BrowerInterface)) {
$className = __NAMESPACE__ . '\\' . ucfirst($name) . 'Brower';
if (class_exists($className) && (new \ReflectionClass($className))->isSubclassOf(BrowerInterface::class)) {
self::$instances[$name] = new $className();
} else {
self::$instances[$name] = new Brower();
}
}
return self::$instances[$name];
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-7-13
* Time: 上午9:51
*/
namespace common\service\brower;
/**
* Interface BrowerInterface
* 区块链浏览器接口
*
* @package common\service\brower
*/
interface BrowerInterface
{
/**
* @param string $txhash
* @return mixed|string|boolean 交易状态
*/
public function getStatus(string $txhash);
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-7-13
* Time: 上午10:17
*/
namespace common\service\brower;
use common\helpers\Curl;
class BtcBrower extends Brower
{
private $base_uri = 'https://btc.com/';
public function getStatus(string $txhash)
{
$uri = $this->base_uri . $txhash;
$content = (new Curl())->get($uri, true);
if ($content) {
preg_match_all('/<dt> Confirmations<\/dt>(.*?)<dd>(.*?)<\/dd>/is', $content, $matchs);
if (isset($matchs[2][0])) {
return (int)trim($matchs[2][0]);
}
}
return 0;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-7-13
* Time: 上午10:19
*/
namespace common\service\brower;
use common\helpers\Curl;
class EthBrower extends Brower
{
private $base_uri = 'https://etherscan.io/';
public function getStatus(string $txhash)
{
$uri = $this->base_uri . 'tx/' . $txhash;
$ch = new Curl();
$content = $ch->get($uri, true);
if ($content) {
if (strpos($content, 'Success') !== false) {
return 'Success';
} elseif (strpos($content, 'Fail') !== false) {
return 'Fail';
}
}
return false;
}
}
\ No newline at end of file
#### 说明 ####
提供区块链浏览器爬取交易状态的功能
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment