PHP中的file_get_contents无法正常工作

时间:2012-04-17 22:14:10

标签: php screen file-get-contents scrape

对于课堂项目,我们应该从其他网站提取数据。我从GiantBomb.com选择了类似于给定游戏的游戏(在我的剧本中给予他们信用)

// $gameLink = "/call-of-duty-black-ops/61-26423/";
$html = file_get_contents("http://www.giantbomb.com" . urlencode($gameLink) . "games_similar/");
echo $html;

这不会返回任何内容。

但是,如果手动输入$ gameLink:

$html = file_get_contents("http://www.giantbomb.com/call-of-duty-black-ops/61-26423/games_similar/");
echo $html;

现在,它将正确返回结果。我的代码出了什么问题?我尝试在整个链接上执行urlencode()而不仅仅是$ gameLink变量,但它仍然失败。有没有人有任何建议?

2 个答案:

答案 0 :(得分:5)

您只需要在html中嵌入urlencode链接。不要将urlencode链接传递给file_get_contents()。

答案 1 :(得分:0)

我认为这是因为urlencode()会编码除-_之外的所有非字母数字字符。

这意味着你的正斜杠被编码到%2F中,这不是你想要的;)但是,正如hegemon上面提到的那样,不要将urlencode链接传递给file_get_contents()。