1
talunceford
Need some coding help here..... cUrl specifically....

Hey guys gals, I just switched from hosting from my home to a dedicated hosting firm. One of my custom scripts is now broke, why might you ask? Because they don't allow url_fopen. I can understand this, as it is a problematic security flaw in php. Ok, they suggested using cUrl to solve the issues with my script. I don't have any experience with cUrl, so I am leaning on the great community of XOOPS to help a fellow Xoopser out. I am going to provide the existing code so that you can look at it and ponder on it some. If there is a class within XOOPS that can handle the incompatibilities that url_fopen is causing on my script, could someone point me in the right direction on where I might find it, and maybe an example of usage.

Guys/gals, I really appreciate your time and any answers or solutions you might have to help me out.

Here is the code that I am currently using. (which doesn't work... obviously... LOL)


Basically what this portion of the script does is fetch data from Gamespy's stats server and creates an xml file from it.

I then parse the code and stick the values in the database so that I can display the stats in the users profile. Thats the brief explanation.


include("config.php");
if    (isset(
$_GET['type'])){ 
$type $_GET['type']; 
if(
$type != "png" && $type != "gif" && $type != "jpg" && $type != "jpeg"){ 
$type "jpg"; } 
}else{ 
$type "jpg"
}
//#########################################################################################################################################//
header("Expires: ".date("D, d M Y H:i:s",time()+$cachetime)." GMT");
//#########################################################################################################################################//
header('Content-Control: cache');
if    (
$type == "jpg" || $type == "jpeg"){ 
header("Content-type: image/jpeg"); // expire in 6 hours; ); 
imagejpeg($image_out,"",$jpeg_quality); 
}elseif    (
$type == "gif"){
header("Content-type: image/gif"); // expire in 6 hours; ); 
imagegif($image_out); 
}else{ 
header('Content-Type: image/png'); // expire in 6 hours; 
imagepng($image_out); 
}
header("Content-Length: " ob_get_length()); 

//header("Content-type: image/png");

function xml2array($text) {
    
$reg_exp '/<(.*?)>(.*?)/s';
    
preg_match_all($reg_exp$text$match);
    foreach (
$match[1] as $key=>$val) {
        if ( 
preg_match($reg_exp$match[2][$key]) ) {
            
$array[$val][] = xml2array($match[2][$key]);
        } else {
            
$array[$val] = $match[2][$key];
        }
    }
    return 
$array;


//handy dandy little function for making the string centered, and making it fit.
function imagestringcentered ($img,$font,$xoffset,$cy,$text,$color) {
 while (
strlen($text) * imagefontwidth($font) > imagesx($img)-$xoffset) {
  if (
$font 1) { $font--; }
  else { break; }
 }  
 
imagestring($img,$font,(imagesx($img)-$xoffset) / strlen($text) * imagefontwidth($font) / $xoffset,$cy,$text,$color);
}
//Map converter
function mapconverter($mapnumber) {
$mapname[0] = "Kubra Dam";
$mapname[1] = "Mashtuur City";
$mapname[2] = "Operation Clean Sweep";
$mapname[3] = "Zatar Wetlands";
$mapname[4] = "Strike at Karkand";
$mapname[5] = "Sharqi Peninsula";
$mapname[6] = "Gulf of Oman";

$mapname[100] = "Daqing Oilfields";
$mapname[101] = "Dalian Plant";
$mapname[102] = "Dragon Valley";
$mapname[103] = "FuShe Pass";
$mapname[104] = "Wake Island";
$mapname[105] = "Songhua Stalemate";
//Special Forces Maps
$mapname[300] = "Devil's Perch (BF2SF)";
$mapname[301] = "The Iron Gator (BF2SF)";
$mapname[302] = "Night Flight (BF2SF)";
$mapname[303] = "Warlord (BF2SF)";
$mapname[304] = "Leviathan (BF2SF)";
$mapname[305] = "Mass Destruction (BF2SF)";            
$mapname[306] = "Surge (BF2SF)";
$mapname[307] = "Ghost Town (BF2SF)";
//Euro Forces Maps (EF)
$mapname[10] = "Operation Smoke Screen (EF)";
$mapname[11] = "The Great Wall of China (EF)";
$mapname[110] = "Tabara Quarry (EF)";
//Armoured Fury Maps (AF)
/*
    # US Theatre
    "midnight_sun"        : "200",
    "operationroadrage"    : "201",
    "operationharvest"    : "202",
*/
$mapname[200] = "Midnight Sun (AF)";
$mapname[201] = "Operation Road Rage (AF)";
$mapname[202] = "Operation Harvest (AF)";
return 
$mapname[$mapnumber];
}

//Kit converter
function kitconverter($kitnumber) {
$kitname[0] = "Anti-Tank";
$kitname[1] = "Assault";
$kitname[2] = "Engineer";
$kitname[3] = "Medic";
$kitname[4] = "Spec-Ops";
$kitname[5] = "Support";
$kitname[6] = "Sniper";
return 
$kitname[$kitnumber];
}

//Vehicle Converter
function vehicleconverter($vehiclenumber) {
$vehiclename[0] = "Tank";
$vehiclename[1] = "Plane";
$vehiclename[2] = "Air Defense";
$vehiclename[3] = "Helicopter";
$vehiclename[4] = "Transport";
$vehiclename[5] = "Artillery";
$vehiclename[6] = "Ground Defense";
return 
$vehiclename[$vehiclenumber];
}

//Weapon Converter
function weaponconverter($weaponnumber) {
$weaponname[0] = "Assault Rifles";
$weaponname[1] = "Grenade Launchers";
$weaponname[2] = "Carbines";
$weaponname[3] = "Light Machine Guns";
$weaponname[4] = "Sniper Rifles";
$weaponname[5] = "Pistols";
$weaponname[6] = "Rockets";
$weaponname[7] = "Submachine Guns";
$weaponname[8] = "Shotguns";
$weaponname[9] = "Knife";
$weaponname[10] = "Defibrillator";
$weaponname[11] = "Explosives";
$weaponname[12] = "Hand Grenade";
$weaponname[13] = "Tear Gas";
return 
$weaponname[$weaponnumber];
}

$pid $_GET["id"]; 
$mode $_GET["image"];
$theme $_GET["theme"];
if (
$mode == "") {
    
$mode $defaultmode;
}

if (
$pid == "") {
    
/*$error = "No PID Specified";
    $mode = "error";
    include("themes/baseline/theme.php"); 
    exit;*/
    
$pid $defaultpid//Player ID
}
if (
$AllowedPID[0] != "any") {
    if (!
in_array($pid$AllowedPID)) {
        
$error "PID $pid Not Authorized";
        
$mode "error";
        include(
"themes/baseline/theme.php"); 
        exit;
    }
}
$cachefilename $statdir.$pid.".xml";
if ((!
file_exists($cachefilename)) || ((time() - filemtime($cachefilename)) > $cachetime)) {

//$filename = "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?weapon=".$weaponnumbers."&kit=".$kitnumbers."&map=".$mapnumbers."&army=".
$filename "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=per*,cmb*,twsc,cpcp,cacp,dfcp,kila,heal,rviv,rsup,rpar,tgte,dkas,dsab,cdsc,rank,cmsc,kick,kill,deth,suic,ospm,klpm,klpr,dtpr,bksk,wdsk,bbrs,tcdr,ban,dtpm,lbtl,osaa,vrk,tsql,tsqm,tlwf,mvks,vmks,mvn*,vmr*,fkit,fmap,fveh,fwea,wtm-,wkl-,wdt-,wac-,wkd-,vtm-,vkl-,vdt-,vkd-,vkr-,atm-,awn-,alo-,abr-,ktm-,kkl-,kdt-,kkd-";
//$armynumbers."&vehicle=".$vehiclenumbers."&pid=".$pid."&info=".$infotoget.",rank"; 
ini_set("user_agent","GameSpyHTTP/1.0");
$fp fopen ($filename"r");
        if (!
$fp) {
        
//Old Error Handler left incase someone wants it to do this instead of just reading the cache
        /*$error = "BF2 Stats Site Down.";
        $mode = error;
        include("themes/baseline/theme.php");
        exit;
        */
        
if (!file_exists($cachefilename)) {
        
$error "BF2Web Down and no cache file exists.";
        
$mode error;
        include(
"themes/".$theme."/theme.php");
        exit;
        }
        
//read the cache for the stats
    
$xmlfile = @fopen($cachefilename"r");
    if (!
$xmlfile) {
        
$error "Error Reading $cachefilename";
        
$mode "error";
        include(
"themes/".$theme."/theme.php"); 
        exit;
    }
    while((
$xmlfile)) {
        
$xmldoc .= fread($xmlfile,4096);
    }
    
fclose($xmlfile);
    
$thestats xml2array($xmldoc);        
    
//end code for reading stat cache
    
$namedstats $thestats['stats'][0]['info'][0];
    
$namedawards $thestats['stats'][0]['awards'][0];
    
$namedleader $thestats['stats'][0]['leaderboard'][0];
    
$namedunlocks $thestats['stats'][0]['unlocks'][0];
    
$namedmaps $thestats['stats'][0]['maps'][0];
        
//end code for reading stat cache
        
} else {
        
$_xml ="1.0"?>rn  rn    rn";
        while ((
$fp)) { 
            
$content fgets$fp4096 ); 
            
//get names 
            
if (ereg("Et107"$content) == true ) {
            
$error "Error Invalid PID";
            
$mode "error";
            include(
"themes/".$theme."/theme.php"); 
            exit;
            }
            if (
ereg("Htpid"$content) == true ) {
            
$statnames explode("t",$content); //load up the names of the stats so that they can be used for the naming of the array
               
}            
            
//real stats 
            //start the first part of the xml file

            
if (ereg($pid$content) == true ) {
                
$stats explode("t",$content); //make the array with the stats
                //it goes to one less than the count becuase the last one is one i added since the last one always gets messed up for some reason
                //it also skips the first one because that is H (not a stat) from using explode it just picks this one up
                
for($i 1$i<count($stats)-1$i++ ) {
                    
$namedstats[$statnames[$i]] = "$stats[$i]"//add the stat into a named array so its more easily configured.
                    //begin xml generation code                
                    
$_xml .="t ".$statnames[$i].">" $stats[$i] . ".$statnames[$i].">rn";
                }
            }
        }
            
fclose ($fp);
        
$_xml .="    rn    rn";
        }
        
//award part
        
$awardfilename "http://bf2web.gamespy.com/ASP/getawardsinfo.aspx?pid=".$pid;
        
$afp = @fopen ($awardfilename"r");
        if (!
$afp) {
        } else {
        while ( (
$afp)) { 
            
$content fgets$afp4096 );
            if (
ereg("Dt([0-9]{7})t([0-9]*)"$content$awards)) {
                
//echo "1:".$awards[1]." 2:".$awards[2]."n";
                
$namedawards[$awards[1]] = $awards[2]; //add the stat into a named array so its more easily configured.
                //begin xml generation code
                
$_xml .="t  <".$awards[1].">" $awards[2] . ".$awards[1].">rn";
            }
            }
        }
            
fclose ($afp);
            
            
            
        
$_xml .="    rn    rn"
######################################################################################################################################


        //maps part
        
$mapfilename "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=mtm-,mwn-,mls-"
        
$mfp = @fopen ($mapfilename"r");
while ((
$mfp)) { 
            
$mapcontent = @fgets$mfp4096 ); 
            
//get names 
            
if (ereg("Et107"$mapcontent) == true ) {
            
$error "Error Invalid PID";
            
$mode "error";
            include(
"themes/".$theme."/theme.php"); 
            exit;
            }
            if (
ereg("Htpid"$mapcontent) == true ) {
            
$mapnames explode("t",$mapcontent); //load up the names of the stats so that they can be used for the naming of the array
               
}            
            
//real stats 
            //start the first part of the xml file

            
if (ereg($pid$mapcontent) == true ) {
                
$mapstats explode("t",$mapcontent); //make the array with the stats
                //it goes to one less than the count becuase the last one is one a added since the last one always gets messed up for some reason
                //it also skips the first one because that is H (not a stat) from using explode it just picks this one up
                
for($a 1$a<count($mapstats)-1$a++ ) {
                    
$namedmaps[$mapnames[$a]] = "$mapstats[$a]"//add the stat into a named array so its more easily configured.
                    //begin xml generation code                
                    
$_xml .="t ".$mapnames[$a].">" $mapstats[$a] . ".$mapnames[$a].">rn";
                }
            }
        }
            
fclose ($mfp);
        
$_xml .="    rn    rn";
        
        
        
##############################################################################################################################                    
        //Unlocks  part 
        
$unlocksfilename "http://bf2web.gamespy.com/ASP/getunlocksinfo.aspx?pid=".$pid
        
$ufp = @fopen ($unlocksfilename"r"); 
        if (!
$ufp) { 
        } else { 
        while ((
$ufp)) { 
            
$content fgets$ufp4096 ); 
            if (
ereg("Dt([0-9]{2}|[0-9]{3})t([ns])"$content$unlocks)) { 
                
$namedunlocks[$unlocks[1]] = $unlocks[2]; //add the stat into a named array so its more easily configured. 
                //begin xml generation code 
                
$_xml .="t  <".$unlocks[1].">" $unlocks[2] . ".$unlocks[1].">rn"
            } 
        } 
        } 
    
fclose ($ufp); 
            
            
        
$_xml .="    rn    rn";
        
//end awards
        //leaderboard part
        
$leaderfilename "http://bf2web.gamespy.com/ASP/getleaderboard.aspx?pid=".$pid."&type=score&id=overall";
        
$lfp = @fopen ($leaderfilename"r");
        if (!
$lfp) {
        } else {
        while ((
$lfp)) { 
            
$content fgets$lfp4096 );
            if (
ereg("Htntpid"$content) == true ) {
                
$leadernames explode("t",$content); //load up the names of the stats so that they can be used for the naming of the array
               
}            
            
//real stats 
            //start the first part of the xml file

            
if (ereg($pid$content) == true ) {
                
$leader explode("t",$content); //make the array with the stats
                //it also skips the first one because that is H (not a stat) from using explode it just picks this one up
                
for($i 1$i<count($leader); $i++ ) {
                    if (
$i == (count($leader)-1)) {
                    
//this gets rid of the line break the last one has
                    
$leader[$i] = str_replace("n"""$leader[$i]);
                    
$leadernames[$i] = str_replace("n"""$leadernames[$i]);
                    }
                    
$namedleader[$leadernames[$i]] = "$leader[$i]"//add the stat into a named array so its more easily configured.
                    //begin xml generation code                
                    
$_xml .="t  ".$leadernames[$i].">" $leader[$i] . ".$leadernames[$i].">rn";
                }
            }
            }
        }
            
fclose ($lfp);
        
//end leaderboard
        

            
$_xml .="    rn  rn";
        
$xmlcachefile = @fopen($cachefilename"w");
        if (!
$xmlcachefile) {
            
$error "Error Opening $cachefilename for writing";
            
$mode "error";
            include(
"themes/".$theme."/theme.php"); 
            exit;
        }

            if(
fwrite($xmlcachefile$_xml) === False) {
                
$error "Error Writing $cachefilename";
                
$mode "error";
                include(
"themes/".$theme."/theme.php"); 
                exit;
            }
            
fclose($xmlcachefile);






        
} else {
        
//read the cache for the stats
    
$xmlfile = @fopen($cachefilename"r");
    if (!
$xmlfile) {
        
$error "Error Reading $cachefilename";
        
$mode "error";
        include(
"themes/".$theme."/theme.php"); 
        exit;
    }
    while((
$xmlfile)) {
        
$xmldoc .= fread($xmlfile,4096);
    }
    
fclose($xmlfile);
    
$thestats xml2array($xmldoc);        
        
//end code for reading stat cache
    
$namedstats $thestats['stats'][0]['info'][0];
    
$namedawards $thestats['stats'][0]['awards'][0];
    
$namedleader $thestats['stats'][0]['leaderboard'][0];
    
$namedunlocks $thestats['stats'][0]['unlocks'][0];
    
$namedmaps $thestats['stats'][0]['maps'][0];



    }



This code also includes a security measure to prevent people from getting the stats without having a membership, which is handy.

If you find any solutions to the problem, please get ahold of me. I will definately add you to the credits.


Thanks again fellow Xoopsers, I really appreciate your help.

Tim

2
talunceford
Re: Need some coding help here..... cUrl specifically....

Ok will this work?

$filename "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=per*,cmb*,twsc,cpcp,cacp,dfcp,kila,heal,rviv,rsup,rpar,tgte,dkas,dsab,cdsc,rank,cmsc,kick,kill,deth,suic,ospm,klpm,klpr,dtpr,bksk,wdsk,bbrs,tcdr,ban,dtpm,lbtl,osaa,vrk,tsql,tsqm,tlwf,mvks,vmks,mvn*,vmr*,fkit,fmap,fveh,fwea,wtm-,wkl-,wdt-,wac-,wkd-,vtm-,vkl-,vdt-,vkd-,vkr-,atm-,awn-,alo-,abr-,ktm-,kkl-,kdt-,kkd-";
//$armynumbers."&vehicle=".$vehiclenumbers."&pid=".$pid."&info=".$infotoget.",rank"; 
ini_set("user_agent","GameSpyHTTP/1.0");

//the site or URL to get
$fp curl_init($filename);
//set the options for the transfer
curl_setopt($fpCURLOPT_HEADER0);
//execute the session
//curl_exec($ch);

Ok, do I still have to execute the curl session?

//$fp = fopen ($filename, "r");
        
if (!$fp) {
        
//Old Error Handler left incase someone wants it to do this instead of just reading the cache
        /*$error = "BF2 Stats Site Down.";
        $mode = error;
        include("themes/baseline/theme.php");
        exit;
        */
        
if (!file_exists($cachefilename)) {
        
$error "BF2Web Down and no cache file exists.";
        
$mode error;
        include(
"themes/".$theme."/theme.php");
        exit;
        }
        
//read the cache for the stats
    
$xmlfile = @fopen($cachefilename"r");
    if (!
$xmlfile) {
        
$error "Error Reading $cachefilename";
        
$mode "error";
        include(
"themes/".$theme."/theme.php"); 
        exit;
    }
    while((
$xmlfile)) {
        
$xmldoc .= fread($xmlfile,4096);
    }
    
fclose($xmlfile);
    
    
$thestats xml2array($xmldoc);        
    
//end code for reading stat cache
    
$namedstats $thestats['stats'][0]['info'][0];
    
$namedawards $thestats['stats'][0]['awards'][0];
    
$namedleader $thestats['stats'][0]['leaderboard'][0];
    
$namedunlocks $thestats['stats'][0]['unlocks'][0];
    
$namedmaps $thestats['stats'][0]['maps'][0];
        
//end code for reading stat cache
        
} else {
        
$_xml ="1.0"?>rn  rn    rn";
        while ((
$fp)) { 
            
$content fgets$fp4096 ); 
            
//get names 
            
if (ereg("Et107"$content) == true ) {
            
$error "Error Invalid PID";
            
$mode "error";
            include(
"themes/".$theme."/theme.php"); 
            exit;
            }
            if (
ereg("Htpid"$content) == true ) {
            
$statnames explode("t",$content); //load up the names of the stats so that they can be used for the naming of the array
               
}            
            
//real stats 
            //start the first part of the xml file

            
if (ereg($pid$content) == true ) {
                
$stats explode("t",$content); //make the array with the stats
                //it goes to one less than the count becuase the last one is one i added since the last one always gets messed up for some reason
                //it also skips the first one because that is H (not a stat) from using explode it just picks this one up
                
for($i 1$i<count($stats)-1$i++ ) {
                    
$namedstats[$statnames[$i]] = "$stats[$i]"//add the stat into a named array so its more easily configured.
                    //begin xml generation code                
                    
$_xml .="t ".$statnames[$i].">" $stats[$i] . ".$statnames[$i].">rn";
                }
            }
        }
            
//fclose ($fp);
//free up system resources !!IMPORTANT
            
curl_close($fp);
        
$_xml .="    rn    rn";
        }

3
talunceford
Re: Need some coding help here..... cUrl specifically....

Good grief.... I guess I need to use the search more often LOL!!!

I found this....

Hosting provider turned off allow_url_fopen PHP configurationsoneeded to replace fopen with curl codeMy modifications to module Stockbox (stockquotesappears to work okayalthough I'm no PHP expert (so the code may not be optimal).

// file xoops/modules/stockquotes/blocks/stockbox_iframe.php
// function get_stock_quote($symbol) modified for curl 5-3-2005 jph
// trim is required because $quotes are passed in java script text array using echo

function get_stock_quote($symbol)
{
$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1d1t1c1ohgv" ,$symbol);
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
$csvline = curl_exec($curl_handle);
if (curl_errno($curl_handle) || empty($csvline)) {
curl_close($curl_handle);
return 1;
}
curl_close($curl_handle);
$array = explode(",", $csvline);
$this->symbol = trim($array[0], """); // jph added strip quote
$this->last = trim($array[1]);
$this->date = trim($array[2], """); // jph
$this->time = trim($array[3], """); // jph
$this->change = trim($array[4]);
$this->open = trim($array[5]);
$this->high = trim($array[6]);
$this->low = trim($array[7]);
$this->volume = trim($array[8]);
$this->changepct = 0;
return 0;
}


Referenced article:
https://xoops.org/modules/repository/singlefile.php?lid=1374&com_id=16730&com_rootid=16730&com_mode=thread&#comment16730

4
talunceford
Re: Need some coding help here..... cUrl specifically....

Ok, I modified the code.

Hopefully I won't get any nasty emails saying "WTF are you doing to our server!?!??!?!?!?!"

Here is what a section of the modified code looks like. Will this work?

//leaderboard part
        
$leaderfilename "http://bf2web.gamespy.com/ASP/getleaderboard.aspx?pid=".$pid."&type=score&id=overall";
##########################################  cUrl MODIFICATION ########################################        
$lfp_curl=curl_init();
curl_setopt($lfp_curl,CURLOPT_URL,$leaderfilename);
curl_setopt ($lfp_curlCURLOPT_RETURNTRANSFER1);
curl_setopt ($lfp_curlCURLOPT_CONNECTTIMEOUT2);
$lfp curl_exec($lfp_curl);
##########################################  cUrl MODIFICATION ########################################        
        //$lfp = @fopen ($leaderfilename, "r");
        
if (!$lfp) {
        } else {
        while ((
$lfp)) { 
            
$content fgets$lfp4096 );
            if (
ereg("Htntpid"$content) == true ) {
                
$leadernames explode("t",$content); //load up the names of the stats so that they can be used for the naming of the array
               
}            
            
//real stats 
            //start the first part of the xml file

            
if (ereg($pid$content) == true ) {
                
$leader explode("t",$content); //make the array with the stats
                //it also skips the first one because that is H (not a stat) from using explode it just picks this one up
                
for($i 1$i<count($leader); $i++ ) {
                    if (
$i == (count($leader)-1)) {
                    
//this gets rid of the line break the last one has
                    
$leader[$i] = str_replace("n"""$leader[$i]);
                    
$leadernames[$i] = str_replace("n"""$leadernames[$i]);
                    }
                    
$namedleader[$leadernames[$i]] = "$leader[$i]"//add the stat into a named array so its more easily configured.
                    //begin xml generation code                
                    
$_xml .="t  ".$leadernames[$i].">" $leader[$i] . ".$leadernames[$i].">rn";
                }
            }
            }
        }
curl_close($lfp);
        
//end leaderboard

5
Catzwolf
Re: Need some coding help here..... cUrl specifically....
  • 2007/3/1 20:19

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


Hey Tim,

Can you tell me exactly what the problem is that you are having? I don have time just now to fully look into this for you, but if you can give me more info i will def help you get this working for you.

John

6
talunceford
Re: Need some coding help here..... cUrl specifically....

Hey John, thanks for replying...


My host disabled url_fopen so Im making changes to the code so that I can enable my stats fetching script again. Before I knew that my host didn't allow url_fopen, I ran the script and was filling the error logs quite quickly. I got a pretty ecstatic email from the support staff this morning because of it. Anyway, I can send you the script if you like.

Just let me know, and thanks again for your help.

7
Catzwolf
Re: Need some coding help here..... cUrl specifically....
  • 2007/3/1 20:31

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


Sure i will send you a pm with my email address

Catz

8
talunceford
Re: Need some coding help here..... cUrl specifically....

Sent.

9
talunceford
Re: Need some coding help here..... cUrl specifically....

Ok, I've made some changes and I've got most of the code working, but in doing so, I had to disable some of the other functions that were broke because of the inability of being able to use url_fopen. More specifically, the fgets() and !feof() functions. So I am having to try and find other alternatives to achieve the same result. Ok, basically, it broke the while loop that puts the parsed values in an array. I am needing to find an alternative to it.

I have a partial solution, but it doesn't work all the way.

Basically, what is happening is that it generates the xml file, but it doesn't populate the xml file with data.

I included the xml generation code for you guys to take a look at and see what I am missing as far as the the arrayed values are concerned. I know I am missing something, but I've looked at it so much that I am going blind.

Here is the code:

if    (isset($_GET['type'])){ 
$type $_GET['type']; 
if(
$type != "png" && $type != "gif" && $type != "jpg" && $type != "jpeg"){ 
$type "jpg"; } 
}else{ 
$type "jpg"
}
//#########################################################################################################################################//
header("Expires: ".date("D, d M Y H:i:s",time()+$cachetime)." GMT");
//#########################################################################################################################################//
header('Content-Control: cache');
if    (
$type == "jpg" || $type == "jpeg"){ 
header("Content-type: image/jpeg"); // expire in 6 hours; ); 
imagejpeg($image_out,"",$jpeg_quality); 
}elseif    (
$type == "gif"){
header("Content-type: image/gif"); // expire in 6 hours; ); 
imagegif($image_out); 
}else{ 
header('Content-Type: image/png'); // expire in 6 hours; 
imagepng($image_out); 
}
header("Content-Length: " ob_get_length()); 

//header("Content-type: image/png");

function xml2array($text) {
    
$reg_exp '/<(.*?)>(.*?)/s';
    
preg_match_all($reg_exp$text$match);
    foreach (
$match[1] as $key=>$val) {
        if ( 
preg_match($reg_exp$match[2][$key]) ) {
            
$array[$val][] = xml2array($match[2][$key]);
        } else {
            
$array[$val] = $match[2][$key];
        }
    }
    return 
$array;


//handy dandy little function for making the string centered, and making it fit.
function imagestringcentered ($img,$font,$xoffset,$cy,$text,$color) {
 while (
strlen($text) * imagefontwidth($font) > imagesx($img)-$xoffset) {
  if (
$font 1) { $font--; }
  else { break; }
 }  
 
imagestring($img,$font,(imagesx($img)-$xoffset) / strlen($text) * imagefontwidth($font) / $xoffset,$cy,$text,$color);
}
//Map converter
function mapconverter($mapnumber) {
$mapname[0] = "Kubra Dam";
$mapname[1] = "Mashtuur City";
$mapname[2] = "Operation Clean Sweep";
$mapname[3] = "Zatar Wetlands";
$mapname[4] = "Strike at Karkand";
$mapname[5] = "Sharqi Peninsula";
$mapname[6] = "Gulf of Oman";

$mapname[100] = "Daqing Oilfields";
$mapname[101] = "Dalian Plant";
$mapname[102] = "Dragon Valley";
$mapname[103] = "FuShe Pass";
$mapname[104] = "Wake Island";
$mapname[105] = "Songhua Stalemate";
//Special Forces Maps
$mapname[300] = "Devil's Perch (BF2SF)";
$mapname[301] = "The Iron Gator (BF2SF)";
$mapname[302] = "Night Flight (BF2SF)";
$mapname[303] = "Warlord (BF2SF)";
$mapname[304] = "Leviathan (BF2SF)";
$mapname[305] = "Mass Destruction (BF2SF)";            
$mapname[306] = "Surge (BF2SF)";
$mapname[307] = "Ghost Town (BF2SF)";
//Euro Forces Maps (EF)
$mapname[10] = "Operation Smoke Screen (EF)";
$mapname[11] = "The Great Wall of China (EF)";
$mapname[110] = "Tabara Quarry (EF)";
//Armoured Fury Maps (AF)
/*
    # US Theatre
    "midnight_sun"        : "200",
    "operationroadrage"    : "201",
    "operationharvest"    : "202",
*/
$mapname[200] = "Midnight Sun (AF)";
$mapname[201] = "Operation Road Rage (AF)";
$mapname[202] = "Operation Harvest (AF)";
return 
$mapname[$mapnumber];
}

//Kit converter
function kitconverter($kitnumber) {
$kitname[0] = "Anti-Tank";
$kitname[1] = "Assault";
$kitname[2] = "Engineer";
$kitname[3] = "Medic";
$kitname[4] = "Spec-Ops";
$kitname[5] = "Support";
$kitname[6] = "Sniper";
return 
$kitname[$kitnumber];
}

//Vehicle Converter
function vehicleconverter($vehiclenumber) {
$vehiclename[0] = "Tank";
$vehiclename[1] = "Plane";
$vehiclename[2] = "Air Defense";
$vehiclename[3] = "Helicopter";
$vehiclename[4] = "Transport";
$vehiclename[5] = "Artillery";
$vehiclename[6] = "Ground Defense";
return 
$vehiclename[$vehiclenumber];
}

//Weapon Converter
function weaponconverter($weaponnumber) {
$weaponname[0] = "Assault Rifles";
$weaponname[1] = "Grenade Launchers";
$weaponname[2] = "Carbines";
$weaponname[3] = "Light Machine Guns";
$weaponname[4] = "Sniper Rifles";
$weaponname[5] = "Pistols";
$weaponname[6] = "Rockets";
$weaponname[7] = "Submachine Guns";
$weaponname[8] = "Shotguns";
$weaponname[9] = "Knife";
$weaponname[10] = "Defibrillator";
$weaponname[11] = "Explosives";
$weaponname[12] = "Hand Grenade";
$weaponname[13] = "Tear Gas";
return 
$weaponname[$weaponnumber];
}

$pid $_GET["id"]; 
$mode $_GET["image"];
$theme $_GET["theme"];
if (
$mode == "") {
    
$mode $defaultmode;
}

if (
$pid == "") {
    
/*$error = "No PID Specified";
    $mode = "error";
    include("themes/baseline/theme.php"); 
    exit;*/
    
$pid $defaultpid//Player ID
}
if (
$AllowedPID[0] != "any") {
    if (!
in_array($pid$AllowedPID)) {
        
$error "PID $pid Not Authorized";
        
$mode "error";
        include(
"themes/baseline/theme.php"); 
        exit;
    }
}
$cachefilename $statdir.$pid.".xml";
if ((!
file_exists($cachefilename)) || ((time() - filemtime($cachefilename)) > $cachetime)) {






//$filename = "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?weapon=".$weaponnumbers."&kit=".$kitnumbers."&map=".$mapnumbers."&army=".
//$filename = "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=per*,cmb*,twsc,cpcp,cacp,dfcp,kila,heal,rviv,rsup,rpar,tgte,dkas,dsab,cdsc,rank,cmsc,kick,kill,deth,suic,ospm,klpm,klpr,dtpr,bksk,wdsk,bbrs,tcdr,ban,dtpm,lbtl,osaa,vrk,tsql,tsqm,tlwf,mvks,vmks,mvn*,vmr*,fkit,fmap,fveh,fwea,wtm-,wkl-,wdt-,wac-,wkd-,vtm-,vkl-,vdt-,vkd-,vkr-,atm-,awn-,alo-,abr-,ktm-,kkl-,kdt-,kkd-";
//$armynumbers."&vehicle=".$vehiclenumbers."&pid=".$pid."&info=".$infotoget.",rank"; 
//ini_set("user_agent","GameSpyHTTP/1.0");
//$fp = @fopen ($filename, "r");
#################################################################  CURL MODIFICATION  ###########################################################
$ch curl_init();
$header[]="User-Agent: GameSpyHTTP/1.0rn";
//$header[]="Connection: closern";
curl_setopt($chCURLOPT_URL"http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=per*,cmb*,twsc,cpcp,cacp,dfcp,kila,heal,rviv,rsup,rpar,tgte,dkas,dsab,cdsc,rank,cmsc,kick,kill,deth,suic,ospm,klpm,klpr,dtpr,bksk,wdsk,bbrs,tcdr,ban,dtpm,lbtl,osaa,vrk,tsql,tsqm,tlwf,mvks,vmks,mvn*,vmr*,fkit,fmap,fveh,fwea,wtm-,wkl-,wdt-,wac-,wkd-,vtm-,vkl-,vdt-,vkd-,vkr-,atm-,awn-,alo-,abr-,ktm-,kkl-,kdt-,kkd-
"
);
curl_setopt($chCURLOPT_HEADERtrue);
curl_setopt($chCURLOPT_HTTPHEADER,$header);
curl_setopt($chCURLOPT_RETURNTRANSFER,true);
$fp=curl_exec($ch);
curl_close ($fp);
#################################################################  CURL MODIFICATION  ##########################################################

        
if (!$fp) {
        
//Old Error Handler left incase someone wants it to do this instead of just reading the cache
        /*$error = "BF2 Stats Site Down.";
        $mode = error;
        include("themes/baseline/theme.php");
        exit;
        */
        
if (!file_exists($cachefilename)) {
        
$error "BF2Web Down and no cache file exists.";
        
$mode error;
        include(
"themes/".$theme."/theme.php");
        exit;
        }
        
//read the cache for the stats
    
$xmlfile = @fopen($cachefilename"r");
    if (!
$xmlfile) {
        
$error "Error Reading $cachefilename";
        
$mode "error";
        include(
"themes/".$theme."/theme.php"); 
        exit;
    }
    while(!
feof($xmlfile)) {
        
$xmldoc .= fread($xmlfile,4096);
    }
    
fclose($xmlfile);
    
$thestats xml2array($xmldoc);        
    
//end code for reading stat cache
    
$namedstats $thestats['stats'][0]['info'][0];
    
$namedawards $thestats['stats'][0]['awards'][0];
    
$namedleader $thestats['stats'][0]['leaderboard'][0];
    
$namedunlocks $thestats['stats'][0]['unlocks'][0];
    
$namedmaps $thestats['stats'][0]['maps'][0];
        
//end code for reading stat cache
        
} else {
        
$_xml ="1.0"?>rn  rn    rn";
        
//while (($fp)) { 
        //$content = fgets( $fp, 4096 ); 
        //$content=preg_split("/n/",$fp);
        ############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################
        
$content = array($fp);
        
$content explode("t"$fp);
        
## array test
        //$statnames = print_r(array_values($content));
        ############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################
        //echo $statnames;
            //get names 
            
if (ereg("Et107"$content) == true ) {
            
$error "Error Invalid PID";
            
$mode "error";
            include(
"themes/".$theme."/theme.php"); 
            exit;
            }
            if (
ereg("Htpid"$content) == true ) {
            
$statnames $content//load up the names of the stats so that they can be used for the naming of the array
               
}            
            
//real stats 
            //start the first part of the xml file

            
if (ereg($pid$content) == true ) {
                
$stats $content//make the array with the stats
                //it goes to one less than the count becuase the last one is one i added since the last one always gets messed up for some reason
                //it also skips the first one because that is H (not a stat) from using explode it just picks this one up
                
for($i 1$i<count($stats)-1$i++ ) {
                    
$namedstats[$statnames[$i]] = "$stats[$i]"//add the stat into a named array so its more easily configured.
                    //begin xml generation code                
                    
$_xml .="t ".$statnames[$i].">" $stats[$i] . ".$statnames[$i].">rn";
                }
            }
        
//}

        
$_xml .="    rn    rn";
        }
        
//award part
        //$awardfilename = "http://bf2web.gamespy.com/ASP/getawardsinfo.aspx?pid=".$pid;
        //$afp = @fopen ($awardfilename, "r");

#################################################################  CURL MODIFICATION  ###########################################################
$ch curl_init();
//$header[]="User-Agent: GameSpyHTTP/1.0rn";
//$header[]="Connection: closern";
curl_setopt($chCURLOPT_URL"http://bf2web.gamespy.com/ASP/getawardsinfo.aspx?pid=".$pid);
curl_setopt($chCURLOPT_HEADERtrue);
curl_setopt($chCURLOPT_HTTPHEADER,$header);
curl_setopt($chCURLOPT_RETURNTRANSFER,true);
$afp=curl_exec($ch);
curl_close ($afp);
#################################################################  CURL MODIFICATION  ##########################################################
        
if (!$afp) {
        } else {
        
//while (!feof ($afp)) { 
            //$content = fgets( $afp, 4096 );
                    ############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################
        
$content = array();
        
$content explode("t"$afp);
        
############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################

            
if (ereg("Dt([0-9]{7})t([0-9]*)"$content$awards)) {
                
//echo "1:".$awards[1]." 2:".$awards[2]."n";
                
$namedawards[$awards[1]] = $awards[2]; //add the stat into a named array so its more easily configured.
                //begin xml generation code
                
$_xml .="t  <".$awards[1].">" $awards[2] . ".$awards[1].">rn";
            }
            }
        
//}

            
            
            
        
$_xml .="    rn    rn"
######################################################################################################################################


        //maps part
        //$mapfilename = "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=mtm-,mwn-,mls-"; 
        //$mfp = @fopen ($mapfilename, "r");

#################################################################  CURL MODIFICATION  ###########################################################
$ch curl_init();
//$header[]="User-Agent: GameSpyHTTP/1.0rn";
//$header[]="Connection: closern";
curl_setopt($chCURLOPT_URL"http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=mtm-,mwn-,mls-");
curl_setopt($chCURLOPT_HEADERtrue);
curl_setopt($chCURLOPT_HTTPHEADER,$header);
curl_setopt($chCURLOPT_RETURNTRANSFER,true);
$mfp=curl_exec($ch);
curl_close($mfp);
#################################################################  CURL MODIFICATION  ##########################################################


//while (!feof ($mfp)) { 
            //$mapcontent = fgets( $mfp, 4096 ); 
                    ############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################
        
$mapcontent = array();
        
$mapcontent explode("t",$mfp);
        
############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################

            //get names 
            
if (ereg("Et107"$mapcontent) == true ) {
            
$error "Error Invalid PID";
            
$mode "error";
            include(
"themes/".$theme."/theme.php"); 
            exit;
            }
            if (
ereg("Htpid"$mapcontent) == true ) {
            
$mapnames $mapcontent//load up the names of the stats so that they can be used for the naming of the array
               
}            
            
//real stats 
            //start the first part of the xml file

            
if (ereg($pid$mapcontent) == true ) {
                
$mapstats $mapcontent//make the array with the stats
                //it goes to one less than the count becuase the last one is one a added since the last one always gets messed up for some reason
                //it also skips the first one because that is H (not a stat) from using explode it just picks this one up
                
for($a 1$a<count($mapstats)-1$a++ ) {
                    
$namedmaps[$mapnames[$a]] = "$mapstats[$a]"//add the stat into a named array so its more easily configured.
                    //begin xml generation code                
                    
$_xml .="t ".$mapnames[$a].">" $mapstats[$a] . ".$mapnames[$a].">rn";
                }
            }
        
//}
            
        
$_xml .="    rn    rn";
        
        
        
##############################################################################################################################                    
        //Unlocks  part 
        //$unlocksfilename = "http://bf2web.gamespy.com/ASP/getunlocksinfo.aspx?pid=".$pid; 
        //$ufp = @fopen ($unlocksfilename, "r"); 
#################################################################  CURL MODIFICATION  ###########################################################
$ch curl_init();
//$header[]="User-Agent: GameSpyHTTP/1.0rn";
//$header[]="Connection: closern";
curl_setopt($chCURLOPT_URL"http://bf2web.gamespy.com/ASP/getunlocksinfo.aspx?pid=".$pid);
curl_setopt($chCURLOPT_HEADERtrue);
curl_setopt($chCURLOPT_HTTPHEADER,$header);
curl_setopt($chCURLOPT_RETURNTRANSFER,true);
$ufp=curl_exec($ch);
curl_close ($ufp); 
#################################################################  CURL MODIFICATION  ##########################################################
        
if (!$ufp) { 
        } else { 
        
//while (!feof ($ufp)) { 
            //$content = fgets( $ufp, 4096 ); 
                    ############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################
        
$content = array();
        
$content explode("t"$ufp);
        
############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################

            
if (ereg("Dt([0-9]{2}|[0-9]{3})t([ns])"$content$unlocks)) { 
                
$namedunlocks[$unlocks[1]] = $unlocks[2]; //add the stat into a named array so its more easily configured. 
                //begin xml generation code 
                
$_xml .="t  <".$unlocks[1].">" $unlocks[2] . ".$unlocks[1].">rn"
            } 
        } 
        
//} 
    
            
            
        
$_xml .="    rn    rn";
        
//end awards
        //leaderboard part
        //$leaderfilename = "http://bf2web.gamespy.com/ASP/getleaderboard.aspx?pid=".$pid."&type=score&id=overall";
        //$lfp = @fopen ($leaderfilename, "r");
#################################################################  CURL MODIFICATION  ###########################################################
$ch curl_init();
//$header[]="User-Agent: GameSpyHTTP/1.0rn";
//$header[]="Connection: closern";
curl_setopt($chCURLOPT_URL"http://bf2web.gamespy.com/ASP/getleaderboard.aspx?pid=".$pid."&type=score&id=overall");
curl_setopt($chCURLOPT_HEADERtrue);
curl_setopt($chCURLOPT_HTTPHEADER,$header);
curl_setopt($chCURLOPT_RETURNTRANSFER,true);
$lfp=curl_exec($ch);
curl_close ($lfp);
#################################################################  CURL MODIFICATION  ##########################################################
        
if (!$lfp) {
        } else {
        
//while (!feof ($lfp)) { 
            //$content = fgets( $lfp, 4096 );
                    ############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################
        
$content = array();
        
$content explode("t"$lfp);
        
############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################

            
if (ereg("Htntpid"$content) == true ) {
                
$leadernames $content//load up the names of the stats so that they can be used for the naming of the array
               
}            
            
//real stats 
            //start the first part of the xml file

            
if (ereg($pid$content) == true ) {
                
$leader $content//make the array with the stats
                //it also skips the first one because that is H (not a stat) from using explode it just picks this one up
                
for($i 1$i<count($leader); $i++ ) {
                    if (
$i == (count($leader)-1)) {
                    
//this gets rid of the line break the last one has
                    
$leader[$i] = str_replace("n"""$leader[$i]);
                    
$leadernames[$i] = str_replace("n"""$leadernames[$i]);
                    }
                    
$namedleader[$leadernames[$i]] = "$leader[$i]"//add the stat into a named array so its more easily configured.
                    //begin xml generation code                
                    
$_xml .="t  ".$leadernames[$i].">" $leader[$i] . ".$leadernames[$i].">rn";
                }
            }
            }
        
//}
        //end leaderboard
        

            
$_xml .="    rn  rn";
        
$xmlcachefile = @fopen($cachefilename"w");
        if (!
$xmlcachefile) {
            
$error "Error Opening $cachefilename for writing";
            
$mode "error";
            include(
"themes/".$theme."/theme.php"); 
            exit;
        }

            if(
fwrite($xmlcachefile$_xml) === False) {
                
$error "Error Writing $cachefilename";
                
$mode "error";
                include(
"themes/".$theme."/theme.php"); 
                exit;
            }
            
fclose($xmlcachefile);






        
} else {
        
//read the cache for the stats
    
$xmlfile = @fopen($cachefilename"r");
    if (!
$xmlfile) {
        
$error "Error Reading $cachefilename";
        
$mode "error";
        include(
"themes/".$theme."/theme.php"); 
        exit;
    }
    while(!
feof($xmlfile)) {
        
$xmldoc .= fread($xmlfile,4096);
    }
    
fclose($xmlfile);
    
$thestats xml2array($xmldoc);        
        
//end code for reading stat cache
    
$namedstats $thestats['stats'][0]['info'][0];
    
$namedawards $thestats['stats'][0]['awards'][0];
    
$namedleader $thestats['stats'][0]['leaderboard'][0];
    
$namedunlocks $thestats['stats'][0]['unlocks'][0];
    
$namedmaps $thestats['stats'][0]['maps'][0];



    }


About halfway down the code you will see the modifications that I have made to allow for curl and for the while statement. I am not real sure that they are 100% correct, so if you guys have a better solution, please let me know.


Thanks so much guys,

Login

Who's Online

253 user(s) are online (103 user(s) are browsing Support Forums)


Members: 0


Guests: 253


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits