简体   繁体   English

如果URL是文本文件中的一行,则file_get_contents不起作用

[英]file_get_contents doesn't work if the URL is a line from a text file

Hey I'm new to PHP so this may be an obvious mistake. 嘿,我是PHP的新手,所以这可能是一个明显的错误。 At the moment I am trying to read a score for games from metacritic and display it to the user. 目前我正试图从metacritic中读取游戏的分数并将其显示给用户。 Here is the code I use to do that: 这是我用来做的代码:

$linkToGame= 'METACRITIC LINK';

$opts = array('http' => array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$url = file_get_contents($linktoGame, FALSE, $context);

$first_step = explode( '<span itemprop="ratingValue">' , $url );
$second_step = explode("</span>" , $first_step[1] );

echo $second_step[0];

This does exactly what it is meant to do which is output the game rating from the metacritic url. 这完全符合它的意思,即从metacritic url输出游戏评级。 What I am trying to do now is have it use a link from a text file which contains a list of 115 metacritic links. 我现在要做的是让它使用来自文本文件的链接,其中包含115个元语法链接的列表。

I used this to read the text file. 我用这个来读取文本文件。

$lines = file('metacriticlinks.txt');

and then switched $linkToGame to 然后切换$linkToGame

$linkToGame = $lines[1];

However by doing this, I get the following error: 但是通过这样做,我收到以下错误:

"Warning: file_get_contents( http://www.metacritic.com/game/3ds/steamworld-heist ): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in C:\\xampp\\htdocs\\learn\\getwebcontent.php on line 19" “警告:file_get_contents( http://www.metacritic.com/game/3ds/steamworld-heist ):无法打开流:HTTP请求失败!在C:\\ xampp \\ htdocs \\ learn \\ getwebcontent中找不到HTTP / 1.0 404 .php在第19行“

The weird thing is if I replace $linkToGame with the exact URL it is displaying in the warning, the code runs perfectly and displays the score for that game. 奇怪的是,如果我用警告中显示的确切URL替换$linkToGame ,代码将完美运行并显示该游戏的分数。 I tried reading each line into an array and using an item from an array but that didn't work either and produced the same warning. 我尝试将每一行读入一个数组并使用数组中的项目,但这也不起作用并产生相同的警告。

The oddest part of this issue is that if I use the last line in the text file or in the array (115), the code works as it should. 这个问题最奇怪的部分是,如果我使用文本文件或数组(115)中的最后一行,代码就可以正常工作。 It just doesn't work for any other line except for the final line. 除了最后一行之外,它对任何其他行都不起作用。 What could be the issue? 可能是什么问题?

Many thanks. 非常感谢。

I think you have a newline character attached to all of your filenames except the last one due to the way file() works. 我认为由于file()工作方式,除了最后一个文件名之外,你的所有文件名都附有换行符。

From the php.net site on file() : file()上的php.net站点:

"Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached" “返回数组中的文件。数组的每个元素对应于文件中的一行,换行符仍然附加”

http://php.net/manual/en/function.file.php http://php.net/manual/en/function.file.php

Great description of your problem and your code behavior! 对问题和代码行为的详细描述!

As Daniel pointed out, you can remove this with the trim() function: http://www.w3schools.com/php/func_string_trim.asp 正如Daniel指出的那样,您可以使用trim()函数删除它: http//www.w3schools.com/php/func_string_trim.asp

It looks like you have an extra whitespace in the output. 看起来你在输出中有一个额外的空格。 You have: 你有:

" http://www.metacritic.com/game/3ds/steamworld-heist [WHITESPACE] " http://www.metacritic.com/game/3ds/steamworld-heist [WHITESPACE]

instead of: 代替:

" http://www.metacritic.com/game/3ds/steamworld-heist ". http://www.metacritic.com/game/3ds/steamworld-heist ”。

Try using trim($lines[1]) instead, which will get rid of any preceding or trailing whitespace. 尝试使用trim($lines[1])代替,这将删除任何前面或尾随的空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM