需要用 cURL 代码更改代码 file_get_contents

发布于 2024-12-12 09:40:22 字数 4608 浏览 1 评论 0原文

我需要在没有 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2024-12-19 09:40:22

替换

$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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文