gpt4 book ai didi

php - 在我的网页上用PHP显示API结果

转载 作者:行者123 更新时间:2023-12-03 09:11:16 26 4
gpt4 key购买 nike

我正在尝试使用PHP在我的网页中显示此API的结果。

我不知道这是什么问题,但是目前我的代码无法正常工作? (空白页)

<!DOCTYPE html>
<html>
<body>

<?php
$nethash = File_get_content("http://mattsmines.net/OMC/get.php?id=nethash");
die("{$nethash}");
?>

</body>
</html>

最佳答案

正确的答案是@ Mr.Smith答案。但我想添加的是,使用die将停止输出并导致页面无法打印其余的html。

另外,如果您访问的API被黑客入侵(可能)或崩溃(可能),则您可以使所有用户了解其服务内容,从而使您的页面困惑。

而且curl比fgc更快,因此也许您会发现以下代码更有用。

<?php 
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://mattsmines.net/OMC/get.php?id=nethash',
CURLOPT_TIMEOUT => 5,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
));
$result = curl_exec($ch);

if(substr($result, -3) == 'h/s'){
$result = 'Nethash is crunching @ '.htmlentities($result);
}else{
$result = 'Error fething hash!';
}
?>
<!DOCTYPE html>
<html>
<body>
<p><?php echo $result; ?></p>
</body>
</html>

另外,如果您想从API查询多个ID,则可以将curl包裹在一个函数中,然后直接传递该ID,(我还添加了一个简单的 session 缓存,因此您无需在每次页面加载时都查询API,这会降低速度降低请求并使您的页面变慢。)
<?php 
session_start();

function get_hash($id){
if(!isset($_SESSION[md5($id)])){
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://mattsmines.net/OMC/get.php?id='.$id,
CURLOPT_TIMEOUT => 5,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
));
$result = curl_exec($ch);

if(substr($result, -3) == 'h/s'){
$_SESSION[md5($id)] = htmlentities(ucfirst($id)).' is crunching @ '.htmlentities($result);
}else{
$_SESSION[md5($id)] = 'Error fething hash for '.htmlentities($id);
}
}
return $_SESSION[md5($id)];
}
?>
<!DOCTYPE html>
<html>
<body>
<p><?php echo get_hash('nethash'); ?></p>
<p><?php echo get_hash('some.other.id'); ?></p>
</body>
</html>

编辑(请参阅评论) See it in action :
<?php 
session_start();

/**
* Simple OMC pool API class
*
* @author Lawrence Cherone
* @version 0.01
*/
class omc{
public $totalz = array();

/**
* Calls API and stores result in $_SESSION
*
* @param string $id
* @return string
*/
function call_api($id){
if(!isset($_SESSION[md5($id)])){
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://mattsmines.net/OMC/get.php?id='.$id,
CURLOPT_TIMEOUT => 5,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
));
$_SESSION[md5($id)] = curl_exec($ch);
}
return $_SESSION[md5($id)];
}

/**
* Gets current pool hash speed and stores in $this->totalz scope.
*
* @param string $id
* @return int
*/
function get_pool_hash($id){
if(!isset($this->totalz[$id])){
$result = $this->call_api($id);

if(substr($result, -3) == 'h/s'){
$this->totalz[$id] = (int) $result;
}else{
$this->totalz[$id] = 0;
}
}
return $this->totalz[$id];
}

/**
* Display queried pools as string
*
* @return string
*/
function display(){
$ret = null;
foreach($this->totalz as $pool=>$hashrate){
$ret .= ucfirst($pool).' is crunching @ '.number_format($hashrate).' Mh/s'.'<br>';
}
return $ret;
}

/**
* Calculates the queried pools total hash rate and returns string
*
* @return string
*/
function total_rate(){
return 'Total Hash Rate: '.number_format(array_sum($this->totalz)).' Mh/s';
}

/**
* Query's the total OMC hash rate and returns string
*
* @return string
*/
function total_omc(){
$result = $this->call_api('totalomc');
return 'The total OMC volume is currently: '.number_format(htmlentities($result)).' Mh/s';
}

/**
* Get current mining pool difficulty
*
* @return string
*/
function total_diff(){
$result = $this->call_api('diff');
return 'The current Omnicoin difficulty is currently: '.number_format(htmlentities($result)).' .';
}

}
/**
* Example:
*
* Initialize the object and query the API, by passing the pool ids
*/
$omc = new omc();
$omc->get_pool_hash('nethash');
$omc->get_pool_hash('OMhash');
$omc->get_pool_hash('BLhash');
$omc->get_pool_hash('MOhash');
?>
<!DOCTYPE html>
<html>
<body>
<p><?php echo $omc->display(); ?></p>
<p><?php echo $omc->total_rate();?></p>
<p><?php echo $omc->total_omc();?></p>
<p><?php echo $omc->total_diff();?></p>
</body>
</html>

另外,由于您想多次调用API,因此应该利用curl_multi一次执行所有这些请求,这将加快API请求的速度。
Gist - curl_multi version In Action

关于php - 在我的网页上用PHP显示API结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com