cURL in place of fopen
Posted by: joon
Posted on: 2006-02-01 12:10:00
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 }