问题
I need this code done without file_get_contents, because my server doesn't allow to change php.ini . cURL works good. So maybe someone can make it work with cURL ?
this is the code i have right now: (A guy once helped me make this and it worked, but now i have new host and it won't let me change allow_url_open)
// List of players to fetch data for
$players = array('FixAlot','Kringel');
// URL to get statistics from
define('LOL_URL', 'http://euw.leagueoflegends.com/ladders/solo-5x5'); // for EU
// To enable caching, create a 'cache' directory in the same directory as
// this script. It should be writable by the php process. The easy way is:
// $ mkdir -m777 cache
// Time to cache results in seconds, 0 for off
define('CACHE_TIME', 60*60*6); // 6h
error_reporting(E_ALL);
function get_player($player_name) {
global $cache_time;
$cache_file = dirname(__file__) . '/cache/' . md5($player_name) . '.dat';
if (CACHE_TIME !== 0 && file_exists($cache_file) && time() - filemtime($cache_file) <= CACHE_TIME){
return unserialize(file_get_contents($cache_file));
}
$page = file_get_contents(LOL_URL);
$html = new DOMDocument;
@$html->loadHTML($page);
$inputs = $html->getElementById('ladders-filter-form')->getElementsByTagName('input');
$post_data = array();
foreach ($inputs as $input){
$post_data[$input->getAttribute('name')] = $input->getAttribute('value');
}
$post_data['player'] = $player_name;
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($post_data),
'headers' => "Referer: ". LOL_URL ."\r\n" .
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.68 Safari/534.30\r\n" .
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" .
"Accept-Charset: UTF-8,*;q=0.5\r\n" .
"Content-Type: application/x-www-form-urlencoded\r\n"
)
));
$page = @file_get_contents(LOL_URL, false, $context);
$html = new DOMDocument;
@$html->loadHTML($page);
$row = NULL;
$rows = $html->getElementsByTagName('tr');
for($i=0;$i<$rows->length;$i++){
if (strpos(@$rows->item($i)->attributes->getNamedItem('class')->nodeValue, 'highlight') !== FALSE){
$row = $rows->item($i);
break;
}
}
if (is_null($row)){
return;
}
$player = array();
$cells = $row->getElementsByTagName('td');
for($i=0;$i<$cells->length;$i++){
$key = str_replace('ladder-field', '', $cells->item($i)->attributes->getNamedItem('class')->nodeValue);
$key = trim($key, ' -');
if ($span = $cells->item($i)->getElementsByTagName('span')->item(0)){
$cells->item($i)->removeChild($span);
}
$player[$key] = trim($cells->item($i)->textContent);
}
$player['icon'] = $row->getElementsByTagName('img')->item(0)->attributes->getNamedItem('src')->nodeValue;
if ($player && file_exists(dirname($cache_file))){
file_put_contents($cache_file, serialize($player));
}
return $player;
}
// make assoc array of players and their data
$players = array_combine($players, array_fill(0, count($players), NULL));
foreach($players as $player_name => $val){
$players[$player_name] = get_player($player_name);
}
?>
<!doctype html>
<head>
<meta charset="utf-8">
<title>Players</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Rank</th>
<th>Player</th>
<th>Wins</th>
<th>Losses</th>
<th>Rating</th>
</tr>
</thead>
<tbody>
<?php foreach($players as $player_name => $data): ?>
<tr>
<td><?php print $data['rank']; ?></td>
<td><?php print $data['player']; ?></td>
<td><?php print $data['wins']; ?></td>
<td><?php print $data['losses']; ?></td>
<td><?php print $data['rating']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
回答1:
Replace
$page = file_get_contents(LOL_URL);
with
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, LOL_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
curl_close($ch);
来源:https://stackoverflow.com/questions/7893211/need-to-change-codes-file-get-contents-with-curl-code