Jump to content

Problem with PHP and cUrl


talunceford

Recommended Posts

Hi guys,

 

Thanks for having such an informative forum. 

 

I have a script that I am having to re-write because of issues with the current host that I am using.  Basically this script uses cUrl to fetch stats from gamespy's stats server based on an unique id and writes the stats to an xml file that is parsed and then the values are stored in a database for querying later and displaying a stats image from as well.  The original version of this script took advantage of url_fopen, and as many of you probably have found out, most hosts have disabled this function in their php.ini files, which more than likely breaks scripts that use it.  As it did mine....  I have got to the point of where I have implemented some cUrl functions and in doing so have eliminated some of the functions that were used in conjunction with url_fopen.  Such as fgets and !eof.  What I am asking help on is for someone to look at the xml generation portion of this script and tell me what I am missing as far as some of the while loops and arrays.  If someone could take a gander at it, I would be VERY appreciative of the help. 

 

Here is the portion of the code that I am having trouble with.  I can get the data from gamespy, and populate an array with it, but after that, I am lost as to what I need to do with that array as far as getting it into the variables that populate the xml generation section of the script. 

 

$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.0\r\n";
//$header[]="Connection: close\r\n";
curl_setopt($ch, CURLOPT_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($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$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 ="<?xml version=\"1.0\"?>\r\n  <stats>\r\n    <info>\r\n";
	//while (($array)) { 
	//$content = fgets( $fp, 4096 ); 
	//$content=preg_split("/\n/",$fp);
	############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################
	## array test
	$content = array();
        $content = explode("\t", $fp);
	//$statnames = print_r(array_values($content));
	############################################  CURL MODIFICATION FOR WHILE STATEMENT ######################
        //echo $statnames;
	    //get names 
	    if (ereg("E\t107", $content) == true ) {
	    $error = "Error Invalid PID";
		$mode = "error";
		include("themes/".$theme."/theme.php"); 
		exit;
	    }
	    if (ereg("H\tpid", $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].">\r\n";
			}
		}
	//}

	$_xml .="    </info>\r\n    <awards>\r\n";
	}
	//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.0\r\n";
//$header[]="Connection: close\r\n";
curl_setopt($ch, CURLOPT_URL, "http://bf2web.gamespy.com/ASP/getawardsinfo.aspx?pid=".$pid);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$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("D\t([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].">\r\n";
		}
		}
	//}




	$_xml .="    </awards>\r\n    <maps>\r\n"; 
######################################################################################################################################


	//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.0\r\n";
//$header[]="Connection: close\r\n";
curl_setopt($ch, CURLOPT_URL, "http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=mtm-,mwn-,mls-");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$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("E\t107", $mapcontent) == true ) {
	    $error = "Error Invalid PID";
		$mode = "error";
		include("themes/".$theme."/theme.php"); 
		exit;
	    }
	    if (ereg("H\tpid", $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].">\r\n";
			}
		}
	//}

	$_xml .="    </maps>\r\n    <unlocks>\r\n";



##############################################################################################################################					
	//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.0\r\n";
//$header[]="Connection: close\r\n";
curl_setopt($ch, CURLOPT_URL, "http://bf2web.gamespy.com/ASP/getunlocksinfo.aspx?pid=".$pid);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$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("D\t([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].">\r\n"; 
		} 
	} 
	//} 



	$_xml .="    </unlocks>\r\n    <leaderboard>\r\n";
	//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.0\r\n";
//$header[]="Connection: close\r\n";
curl_setopt($ch, CURLOPT_URL, "http://bf2web.gamespy.com/ASP/getleaderboard.aspx?pid=".$pid."&type=score&id=overall");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$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("H\tn\tpid", $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].">\r\n";
			}
		}
		}
	//}
	//end leaderboard


		$_xml .="    </leaderboard>\r\n  </stats>\r\n</xml>";
	$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];



}

 

 

Just look for the sections that are commented for cUrl.

 

Also, you can click on this link to see what part of the array data looks like. 

 

http://www.bf2online.com/sigs/curl_test.php

 

 

Thanks so much for your time,

 

Sincerely

 

Tim Lunceford

Link to comment
https://forums.phpfreaks.com/topic/40928-problem-with-php-and-curl/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.