我写了一个简单的脚本来获取页面的内容。当我在Apache服务器上运行它时,我会收到一个错误:
未定义file_get_contents
Apache 2.2,PHP 5.3.9。这是代码:
$url = "http://steamcommunity.com/market/priceoverview/?appid=730&country=PL¤cy=3&market_hash_name=Desert%20Eagle%20%7C%20Blaze%20(Minimal%20Wear)";
$json = file_get_contents($url);
$price = json_decode($json);
alert(price.lowest_price);
如何修复?
如果您是本地用户,这可能是个问题。在php.ini文件中尝试添加
allow_url_fopen = 1
然后重试。如果必须获取文件,我喜欢使用cURL。下面是一个非常简单的远程文件抓取示例。
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
的使用
$returned_content = get_data('http://www.grab.com/grab-me.html');
无论您是否对file_get_contents 有问题,这都可以保证有效