cURL in place of fopen

software development

#1

Hey guys, I’m stuck on getting this guild roster working with cURL. I put some tweaks in there from an example but to translate cURL functions from fopen is way beyond me.

If it’s not too much trouble could some one point out what to replace with what? I’m not sure but would the data be returned in the same way? So when it’s manipulated could I use the same functions to break up the CSV and place it in the table cells.

It’s supposed to be rendered like this. -it’s pointing to my old host within an IFRAME

I don’t know how difficult it would be to anyone experienced with this but if it’s not a big deal could you tell me what to do? Much appreciated!

About 1/2 of the code was removed but this is the most important part.

1 $local_directory = "./"; // this is the directory where your local files 2 // will be written to and read from. Make sure 3 // you have WRITE priveledges on this directory 4 5 $guild_id = 211438; // get this number from the link posted on the 6 // guilddisplay.php page 7 8 // Remember to check the status file so that you are not pulling data 9 // more than once per day 10 // 11 $localstatusfile = $local_directory . "wbp_status.txt"; 12 $infile = fopen ($localstatusfile, "r"); 13 $current_timestamp = 0; 14 if (!$infile) 15 { 16 echo 'No status file available, assuming this is the first run'; 17 } 18 else 19 { 20 // read our status file time 21 $buffer = fgets($infile, 4096); 22 23 $current_timestamp = trim( $buffer ); 24 25 //echo 'Local status file reads : ' . strftime("%m/%d/%y %H:%M:%S",$current_timestamp) . ; 26 } 27 fclose( $infile ); // close our local status file 28 29 $filename = "http://www.warcraftrealms.com/exports/status.txt"; 30 $infile = fopen ($filename, "r"); // open remote status file 31 if (!$infile) 32 { 33 echo 'Unable to open status file.'; 34 exit; 35 } 36 37 $remote_timestamp = 0; 38 if(!feof ($infile)) // only 1 read should be needed for the status file 39 { 40 $buffer = fgets($infile, 4096); 41 $remote_timestamp = trim( $buffer ); 42 43 } 44 fclose( $infile ); // close the remote status file 45 46 if( $remote_timestamp - $current_timestamp > 86400 ) // 1 day = 60*60*24 47 { 48 // 49 // We can do a full get 50 // 51 52 // write our new status file 53 $outfilename = $local_directory . "wbp_status.txt"; 54 $outfile = fopen($outfilename, "w"); 55 if( !$outfile ) 56 { 57 echo 'Unable to open save file => " . $outfilename . "'; 58 exit; 59 } 60 61 fputs($outfile, $buffer); 62 fclose($outfile); 63 64 // 65 // Now get our guild roster file 66 // 67 $filename = 'http://www.warcraftrealms.com/exports/guildexport.php?guildid=' . $guild_id; 68 $infile = fopen ($filename, "r"); 69 if (!$infile) 70 { 71 echo 'Unable to open remote file.\n'; 72 exit; 73 } 74 75 $outfilename = $local_directory . "wbp_guildroster.csv"; 76 $outfile = fopen($outfilename, "w"); 77 if( !$outfile ) 78 { 79 echo 'Unable to open save file => " . $outfilename . "\n'; 80 exit; 81 } 82 83 while (!feof ($infile)) 84 { 85 $buffer = fgets($infile, 4096); 86 fputs($outfile, $buffer); 87 } 88 89 fclose($outfile); 90 fclose($infile); 91 } 92 93 // 94 // Now let's just output our roster as it's given 95 // 96 $filename = $local_directory . "wbp_guildroster.csv"; 97 $infile = fopen ($filename, "r"); 98 if (!$infile) 99 { 100 echo 'Unable to open local roster file.'; 101 exit; 102 } 103 104 // do one read to get the header 105 $buffer = fgets($infile, 4096); 106 107 echo '<table><tr><th>name</th><th>race</th><th>class</th><th>lvl</th><th>last seen</th><th>rank</th></tr>'; 108 109 // read the entries 110 while (!feof ($infile)) 111 { 112 $buffer = fgets($infile, 4096); 113 list( $name, $race, $class, $level, $last_seen, $rank, $pvp_rank ) = explode(",",$buffer); 114 115 { 116 echo '<tr><td>' . $name . '</td><td>' . $race . '</td><td>' . $class . '</td><td>' . $level . '</td><td>' . $last_seen . '</td><td>' . $rank . '</td></tr>'; 117 }
-Joon
{ dvessel }


#2

See if this curl function makes sense. It looks like on line 29 is the reason for curl. If it did not access a different url then curl wouldn’t be needed. I found that out yesterday, thanks to Simon.
BTW, The options may or may not be relavent for what you need. You will find a list of options in the php manual, the link is in the DH wiki under the section on curl.
Silk

IO ($filename=$filename, "r"); function IO ($filename, $io) { $channel=curl_init($location); $file=fopen($filename, $io) or die($php_errormsg); curl_setopt($channel, CURLOPT_FILE, $file); curl_setopt($channel, CURLOPT_HEADER, 0); curl_setopt($channel, CURLOPT_NOBODY, 0); curl_exec($channel); $data=strip_tags(fread($file, filesize($filename))); curl_close($channel); fclose($file) or die($php_errormsg); print "<div><hr><p class=\"view\">View of created document...</p><h1 class=\"data\">$data</h1></div>";}


#3

Yeah, it makes 2 outbound connections. First for a status.txt file to see if a local status file matches. If it doesn’t, it goes out and grabs a CSV file which then has to be manipulated.

So, I think I have 2 problems here. First is grabbing the remote files which I think I can manage from all the examples but transforming that CSV file so it goes into the table cells seems more difficult. I’ll try to get the first part done. i just hope the data gets returned in the same way so I can use the existing functions.

-Joon
{ dvessel }


#4

It looks like everything you need is already in the code you have. A cvs file is suppose to be a list of fields divided up with commas to seperate the data. So as long as the data is being placed into a string, it looks like the code you have will seperate that data and place it into a table.
Thats assuming I am reading that code right :wink:
I am still learning php and I having done any coding with php yet. But I think you are on the right track anyways.
Silk