【问题标题】:php file_get_contents command does not workphp file_get_contents 命令不起作用
【发布时间】:2021-08-12 03:51:37
【问题描述】:

我开始研究从网站中提取数据并找到一些文档并在我的本地主机上运行它们,然后我将代码上传到我的服务器,但数据没有来

freebsd12.2 稳定版

包: nginx-1.20.0_1,2 php80-8.0.6

我的网站代码:

  <?php

function getir($baslangic, $son, $cekilmek_istenen)
{
    @preg_match_all('/' . preg_quote($baslangic, '/') .
    '(.*?)'. preg_quote($son, '/').'/i', $cekilmek_istenen, $m);
    return @$m[1];
}



//Ankr Coin
$url = "https://coinmarketcap.com/currencies/ankr/";

$icerik = file_get_contents($url);

$AnkrCoin = getir('<div class="priceValue___11gHJ">','</div>',$icerik);




//Eose
$url = "https://coinmarketcap.com/currencies/eos/";

$icerik = file_get_contents($url);

$EosCoin = getir('<div class="priceValue___11gHJ">','</div>',$icerik);



//Atom
$url = "https://coinmarketcap.com/currencies/cosmos/";

$icerik = file_get_contents($url);

$CosmosAtom = getir('<div class="priceValue___11gHJ">','</div>',$icerik);




//Mkr
$url = "https://coinmarketcap.com/currencies/maker/";

$icerik = file_get_contents($url);

$Mkr = getir('<div class="priceValue___11gHJ">','</div>',$icerik);
?>

<html>

<head>
  <meta http-equiv="refresh" content="5">
</head>

<body>

  <div id="AnkCoin">
    <p>
      Ankr:<?php echo $AnkrCoin[0]; ?>
    </p>
  </div>

  <div>
    <p>
      Eos: <?php echo $EosCoin[0]; ?>
    </p>
  </div>

  <div>
    <p>
      Cosmos Atom: <?php echo $CosmosAtom[0]; ?>
    </p>
  </div>
  <div>
    <p>
      Mkr: <?php echo $Mkr[0]; ?>
    </p>
  </div>
</body>
</html>

我可以在类似这个命令的表达式中看到 google

<?php
$homepage = file_get_contents('http://www.google.com/');
echo $homepage;
?>

但是当我更改链接时没有任何反应 例如

<?php
$homepage = file_get_contents('https://coinmarketcap.com/currencies/eos/');
echo $homepage;
?>

我的错误日志是干净的。

【问题讨论】:

  • 使用curl 代替file_get_contents(),因为file_get_contents() 不遵循重定向/刷新。但是如果您仍然需要使用file_get_contents(),请检查此php.net/manual/en/context.http.php
  • @Haridarshan 我也尝试 curl 但没有任何改变
  • 我刚刚通过curl 尝试了这个网址https://coinmarketcap.com/currencies/eos/,我得到了回复。那么,您能否分享一下您尝试过的 curl 代码

标签: php nginx freebsd


【解决方案1】:

我自己试过了,它工作正常,得到输出

<?php
function call($url)
{ 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($httpcode !== 200) {
        return null;
    }

    return $output;
}

function getir($baslangic, $son, $cekilmek_istenen)
{
    @preg_match_all('/' . preg_quote($baslangic, '/') .
    '(.*?)'. preg_quote($son, '/').'/i', $cekilmek_istenen, $m);
    return @$m[1];
}


//Ankr Coin
$url = "https://coinmarketcap.com/currencies/ankr/";
$icerik = call($url);
$AnkrCoin = getir('<div class="priceValue___11gHJ">','</div>',$icerik);

//Eose
$url = "https://coinmarketcap.com/currencies/eos/";
$icerik = call($url);
$EosCoin = getir('<div class="priceValue___11gHJ">','</div>',$icerik);



//Atom
$url = "https://coinmarketcap.com/currencies/cosmos/";
$icerik = call($url);
$CosmosAtom = getir('<div class="priceValue___11gHJ">','</div>',$icerik);

//Mkr
$url = "https://coinmarketcap.com/currencies/maker/";
$icerik = call($url);
$Mkr = getir('<div class="priceValue___11gHJ">','</div>',$icerik);
?>

<html>

<head>
  <meta http-equiv="refresh" content="5">
</head>

<body>

  <div id="AnkCoin">
    <p>
      Ankr:<?php echo $AnkrCoin[0]; ?>
    </p>
  </div>

  <div>
    <p>
      Eos: <?php echo $EosCoin[0]; ?>
    </p>
  </div>

  <div>
    <p>
      Cosmos Atom: <?php echo $CosmosAtom[0]; ?>
    </p>
  </div>
  <div>
    <p>
      Mkr: <?php echo $Mkr[0]; ?>
    </p>
  </div>
</body>
</html>

【讨论】:

  • 您的代码正在运行,但我不明白为什么我的代码在本地运行时无法在服务器上运行
  • @hiqermod 你能分享来自服务器的$info 的输出吗
  • 你的意思是 phpinfo(); ?对不起,我是初学者
  • @hiqermod 不,如果你在你的服务器上运行我的代码,这个echo json_encode($info);的输出是什么
  • 我想从另一个我想做的网站剪辑我想要的数据,如上面的代码,现在我与主题疏远了,因为代码的结构发生了变化。有没有办法使用我自己的代码或者我如何在 curl 中做到这一点?
猜你喜欢
  • 2012-08-30
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多