【问题标题】:file_get_contents() doesn't match with url from filefile_get_contents() 与文件中的 url 不匹配
【发布时间】:2020-12-18 07:59:44
【问题描述】:

我想创建一个小型机器人。所以我想从网站上获取 html 文件来扫描它们。当我使用 file_get_contents("an_url_here") 它工作正常,但是当我使用我在文件中记录的 url 时,它会出错:

Warning: file_get_contents("http://schema.org/LocalBusiness "): failed to open stream: No such file or directory in /var/www/private/bot/bot3.php on line 15

我的网址在文件 .txt 中:

"https://www.chrismantcreation.fr"
"http://schema.org/LocalBusiness"
"http://schema.org/WebSite"
"http://www.mon-arbre-ma-genealogie.fr"

这是我的 php 代码:

<?php
$lire_autres_pages = fopen('lien2.txt', 'r');
echo(file_get_contents("https://www.chrismantcreation.fr"); //THIS WORKS

while(!feof($lire_autres_pages)) {
  
  $page_suivante = fgets($lire_autres_pages);
  echo(file_get_contents($page_suivante)); // THIS DOESN'T WORK
}

fclose($lire_autres_pages);

?>

如果有人有一个想法,因为我不明白它为什么会发生。 如果我使用 Curl,我也会遇到同样的问题。

【问题讨论】:

  • 从 $page_suivante 中删除空格和换行符以获得有效的 url
  • 这可能有效:echo (file_get_contents(trim($page_suivante)));.
  • 谢谢,trim($page_suivante) 有效,但字符串不带引号:trim("myurl") 不好,但 trim(myurl) 有效。昨天浪费了很多时间!
  • trim() 有效,因为我认为在最后一行有一个 \n。我不知道 trim(),我试过 preg_replace 来清理我的线条,但它对不可见字符没有任何作用

标签: php bots file-get-contents


【解决方案1】:

所以修剪有效:我的 url 文件(不带引号或双引号):

https://www.chrismantcreation.fr
http://schema.org/LocalBusiness
http://schema.org/WebSite
http://www.mon-arbre-ma-genealogie.fr

我的 php 代码工作:

<?php
$lire_autres_pages = fopen('lien2.txt', 'r');
echo(file_get_contents("https://www.chrismantcreation.fr"); //THIS WORKS

while(!feof($lire_autres_pages)) {
  
  $page_suivante = fgets($lire_autres_pages);
  echo(file_get_contents(trim($page_suivante))); // THIS WORKS !!!!!
}

fclose($lire_autres_pages);

?>

感谢您的回答,祝您有美好的一天!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2021-08-24
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多