0

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 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

but there is also $page = @file_get_contents(LOL_URL, false, $context);
Weren't you asking for a way to replace file_get_contents() by cURL?! with @file_get_contents you are just disabling the error message - I would be greatly surprised if you would be able to download the file like this when allow_url_open is disabled...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.