需要使用cURL代码更改代码file_get_contents

时间:2011-10-25 17:20:51

标签: php caching file-get-contents

我需要在没有file_get_contents的情况下完成此代码,因为我的服务器不允许更改php.ini。 cURL很好用。那么也许有人可以使用cURL吗?

这是我现在的代码:(一个人曾经帮助我做到这一点并且它有效,但现在我有了新主机,它不会让我改变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 个答案:

答案 0 :(得分:3)

替换

$page = file_get_contents(LOL_URL);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, LOL_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
curl_close($ch);