Guild export code with CURL, Please answer in here.

Questions and discussions on development tools for WarcraftRealms
Post Reply
User avatar
shivas
Posts: 3
Joined: Sun Jan 22, 2006 12:21 pm
Contact:

Guild export code with CURL, Please answer in here.

Post by shivas »

Hey guys, I've been using the export code on my old host with no problems but my new one doesn't allow fopen. -for security reasons they claim. I'm no programmer so I hope someone can help show me exactly what needs to be changed so I can work with CURL. I did some tweaking with the numerous examples posted and managed to find a pattern to reassemble with.

I already searched for the answers but there are no real answers for us non-programmers. I really appreciate your solution, thx!

Here's the link to the page.

btw, I started editing the page to use curl but it's still broke, lists the members but it still gives errors. It might be easier for others to follow with the original fopen commands so i left it in the code below just in case anyone else runs into this problem.

Code: Select all

<?php
    //
    //  Rostertest.php
    //
    //  Sample guild export downloader and display
    //  You may use this script as you wish.  This is only some sample code
    //  provided to help get a jumpstart!
    //
    //  Created&#58; 1/27/2005   
    //
    //  Author&#58;  Cooper Sellers aka Rollie - Bloodscalp
    //

    $local_directory = "./";     //  this is the directory where your local files
                                //  will be written to and read from.  Make sure
                                //  you have WRITE priveledges on this directory

    $guild_id        = 308437;   //  get this number from the link posted on the
                                //  guilddisplay.php page
                               


$pvpranktext&#91;1&#93;  = "<i>i.</i> Scout"; 
$pvpranktext&#91;2&#93;  = "<i>ii.</i> Grunt"; 
$pvpranktext&#91;3&#93;  = "<i>iii.</i> Sergeant"; 
$pvpranktext&#91;4&#93;  = "<i>iv.</i> Senior Sergeant"; 
$pvpranktext&#91;5&#93;  = "<i>v.</i> First Sergeant"; 
$pvpranktext&#91;6&#93;  = "<i>vi.</i> Stone Guard"; 
$pvpranktext&#91;7&#93;  = "<i>vii.</i> Blood Guard"; 
$pvpranktext&#91;8&#93;  = "<i>viii.</i> Legionnaire"; 
$pvpranktext&#91;9&#93;  = "<i>ix.</i> Centurion"; 
$pvpranktext&#91;10&#93; = "<i>x.</i> Champion"; 
$pvpranktext&#91;11&#93; = "<i>xi.</i> Lieutenant General"; 
$pvpranktext&#91;12&#93; = "<i>xii.</i> General"; 
$pvpranktext&#91;13&#93; = "<i>xiii.</i> Warlord"; 
$pvpranktext&#91;14&#93; = "<i>xiv.</i> High Warlord"; 

//Activity threshold variables in DAYS 
$idle = 14;     //before $idle days you are active 
$inactive = 30; //after $idle days and before $inacive day you are idle 
                //after $inacive days you are... uhm... inacive 
$seconds_in_a_day = 24*60*60; //self explanitory. 

//Initiallize all counts  
$inactive_count=0; 
$idle_count=0; 
$active_count=0; 

     echo '<head><link href="style.css" rel="stylesheet" type="text/css" /></head></head><body>';
    //
    //  Remember to check the status file so that you are not pulling data
    //  more than once per day
    //
    $localstatusfile = $local_directory . "status.txt";
    $infile = fopen &#40;$localstatusfile, "r"&#41;;
    $current_timestamp = 0;
    if &#40;!$infile&#41;
    &#123;
        echo '<div class="smalltext">No status file available, assuming this is the first run</div>';
    &#125;
    else
    &#123;
        //  read our status file time
        $buffer = fgets&#40;$infile, 4096&#41;;

        $current_timestamp = trim&#40; $buffer &#41;;

        //echo '<div class="smalltext">Local status file reads &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$current_timestamp&#41; . '</div>';
    &#125;
    fclose&#40; $infile &#41;;         //  close our local status file
   
    $filename = "http&#58;//www.warcraftrealms.com/exports/status.txt";
    $infile = fopen &#40;$filename, "r"&#41;;   // open remote status file
    if &#40;!$infile&#41;
    &#123;
        echo '<div class="smalltext">Unable to open status file.</div>';
        exit;
    &#125;

    $remote_timestamp = 0;
    if&#40;!feof &#40;$infile&#41;&#41;   // only 1 read should be needed for the status file
    &#123;
        $buffer = fgets&#40;$infile, 4096&#41;;
        $remote_timestamp = trim&#40; $buffer &#41;;

        //echo '<div class="smalltext">Remote status file reads &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$remote_timestamp&#41; . '</div><br>';

    &#125;
    fclose&#40; $infile &#41;;  //  close the remote status file

    if&#40; $remote_timestamp - $current_timestamp > 86400 &#41; //  1 day = 60*60*24
    &#123;
        //
        //  We can do a full get
        //

        //  write our new status file
        $outfilename = $local_directory . "status.txt";
        $outfile = fopen&#40;$outfilename, "w"&#41;;
        if&#40; !$outfile &#41;
        &#123;
            echo '<div class="smalltext">Unable to open save file => " . $outfilename . "</div>';
            exit;
        &#125;

        fputs&#40;$outfile, $buffer&#41;;
        fclose&#40;$outfile&#41;;

        //
        //  Now get our guild roster file
        //
        $filename = 'http&#58;//www.warcraftrealms.com/exports/guildexport.php?guildid=' . $guild_id;
        $infile = fopen &#40;$filename, "r"&#41;;
        if &#40;!$infile&#41;
        &#123;
            echo '<div class="smalltext">Unable to open remote file.</div>\n';
            exit;
        &#125;

        $outfilename = $local_directory . "guildroster.csv";
        $outfile = fopen&#40;$outfilename, "w"&#41;;
        if&#40; !$outfile &#41;
        &#123;
            echo '<div class="smalltext">Unable to open save file => " . $outfilename . "</div>\n';
            exit;
        &#125;

        while &#40;!feof &#40;$infile&#41;&#41;
        &#123;
            $buffer = fgets&#40;$infile, 4096&#41;;
            fputs&#40;$outfile, $buffer&#41;;
        &#125;

        fclose&#40;$outfile&#41;;
        fclose&#40;$infile&#41;;
    &#125;



    //
    //  Now let's just output our roster as it's given
    //
    $filename = $local_directory . "guildroster.csv";
    $infile = fopen &#40;$filename, "r"&#41;;
    if &#40;!$infile&#41;
    &#123;
        echo '<div class="smalltext">Unable to open local roster file.</div>';
        exit;
    &#125;

    //  do one read to get the header
    $buffer = fgets&#40;$infile, 4096&#41;;

    echo '<div align="center"><table><tr><th>name</th><th>race</th><th>class</th><th>lvl</th><th>last seen</th><th>rank</th><th>pvp rank</th></tr>';

    //  read the entries
    while &#40;!feof &#40;$infile&#41;&#41;
    &#123;
        $buffer = fgets&#40;$infile, 4096&#41;;
        list&#40; $name, $race, $class, $level, $last_seen, $rank, $pvp_rank &#41; = explode&#40;",",$buffer&#41;;
        
        $pvp_rank_t = $pvp_rank-4;

        $member_count++; 
        &#123;
        echo '<tr><td>' . $name . '</td><td>' . $race . '</td><td>' . $class . '</td><td>' . $level . '</td><td>' . $last_seen . '</td><td>' . $rank . '</td><td>' . $pvpranktext&#91;$pvp_rank_t&#93; . '</td></tr>';
        &#125;

        $interval = &#40;&#40;time&#40;&#41; - strtotime&#40;$last_seen&#41;&#41;/86400&#41;; //takes the interval in seconds and converts into days 

        if &#40;$interval < $idle&#41;  &#123;
            $active_count = $active_count + 1;
        &#125;
        else if &#40; $interval >= $idle && $interval < $inactive&#41; &#123;
            $idle_count = $idle_count + 1;
        &#125;
        else if &#40; $interval >= $inactive &#41; &#123;
            $inactive_count = $inactive_count + 1;
        &#125;
    &#125;
    
    $member_count = $member_count-1;
    $active_percent = round&#40;&#40;$active_count/$member_count&#41;*100&#41;;
    $idle_percent = round&#40;&#40;$idle_count/$member_count&#41;*100&#41;;
    $inactive_percent = round&#40;&#40;$inactive_count/$member_count&#41;*100&#41;;

    echo '</table></div>';
    echo '<div class="smalltext" style="text-align&#58;center">';
    echo "$member_count";
    echo ' members / ';
    echo "$active_percent";
    echo '% active / ';
    echo "$idle_percent";
    echo '% idle / ';
    echo "$inactive_percent";
    echo '% inactive';
    echo ' &#40; last seen > 14 days = idle / last seen > 30 days = inative &#41;</div>';

    //  don't forget our credit link =&#41;
    echo '<br><div class="smalltext" style="text-align&#58;center">Last updated ' . strftime&#40;"%A %B %d, %Y - %I&#58;%M&#58;%p",$remote_timestamp&#41; . '</div><br>';
    echo '<br><div class="smalltext">&#91; <a href="http&#58;//www.warcraftrealms.com/guilds/' . $guild_id . '" target="new">list in warcraftrealms.com</a> &#93;&#91; <a href="http&#58;//www.warcraftrealms.com/guildexes/' . $guild_id . '" target="new">ex-members list</a> &#93;</div>';
    echo '<br><div class="smalltext">Guild data provided by <a href="http&#58;//www.warcraftrealms.com/" target="new">WarcraftRealms.com</a>.';
    echo '</div></body>'
?>

Last edited by shivas on Mon Jan 30, 2006 8:27 pm, edited 1 time in total.
-shivas
< The Heartless >
-hosted by Dreamhost

User avatar
Ceto
Shady Dealer
Posts: 335
Joined: Sun Oct 16, 2005 8:22 pm
Location: Plymouth, NH
Contact:

Post by Ceto »

DreamHost is awesome. Regarding curl, I've posted sample code about this before:

http://www.warcraftrealms.com/forum/vie ... =6049#6049

To fix your code, you'll need to reset CURLOPT_URL and do another curl_exec() in place of the fopen() commands. fgets() will be unnecessary. If you'd like to do some error checking, check into the curl_errno() function.

It's really an exercise in programming, though.
Image

User avatar
shivas
Posts: 3
Joined: Sun Jan 22, 2006 12:21 pm
Contact:

Post by shivas »

Yeah, I saw that post but I'm not sure on how to implement it. I'll give it another shot but I really have no clue. :? Thanks for the help though.
-shivas
< The Heartless >
-hosted by Dreamhost

User avatar
shivas
Posts: 3
Joined: Sun Jan 22, 2006 12:21 pm
Contact:

Post by shivas »

I'm having a tough time figuring this out. Aye Ceto, think you could post how you did it? Not just a little snippet but all the code so I can get a better idea on how it all fits together.

And rollie, I know your a busy man but could I get your thoughts on this. If you have a spare moment.. -if that's all it takes. :?

Thanks. :D
-shivas
< The Heartless >
-hosted by Dreamhost

Post Reply