File_get_contents does not work if the URL of a string from a text file

Hey, I'm new to PHP, so this could be an obvious mistake. At the moment, I'm trying to read the rating for games with metacriticism and display it to the user. Here is the code I use for this:

$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 intended to do, which infers the rating of the game from the metacritical URL. What I'm trying to do now is use a link from a text file that contains a list of 115 metacritical links.

I used this to read a text file.

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

and then switch $linkToGameto

$linkToGame = $lines[1];

However, by doing this, I get the following error:

": file_get_contents ( http://www.metacritic.com/game/3ds/steamworld-heist): : HTTP- ! HTTP/1.0 404 C:\xampp\htdocs\learn\getwebcontent.php 19"

, $linkToGame URL-, , . , .

, (115), , . , . ?

.

+4
2

, . :

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

:

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

trim($lines[1]), .

+1

Source: https://habr.com/ru/post/1619769/


All Articles