Hi i have this code can any one tell me how to save this on mysql, also if someone can tell me how to create colums from the result ex. “TEAM” “TEAM ID” etc. Please some help
<?php
class XMLSoccer{
protected $service_url="http://www.xmlsoccer.com/FootballData.asmx";
protected $api_key,$request_ip;
const TIMEOUT_GetLiveScore=25;
const TIMEOUT_GetLiveScoreByLeague=25;
const TIMEOUT_GetOddsByFixtureMatchID=3600;
const TIMEOUT_GetHistoricMatchesByLeagueAndSeason=3600;
const TIMEOUT_GetAllTeams=3600;
const TIMEOUT_GetAllTeamsByLeagueAndSeason=3600;
const TIMEOUT_Others=300;
const TIMEOUT_CURL=30;
public function __construct($api_key=""){
$this->request_ip=gethostbyname(gethostname());
if(empty($this->service_url)) throw new XMLSoccerException("service_url cannot be empty. Please setup");
if(!empty($api_key)) $this->api_key=$api_key;
if(empty($this->api_key)) throw new XMLSoccerException("api_key cannot be empty. Please setup.");
}
/*
list available methods with params.
*/
public function __call($name,$params){
$data=$this->request($this->buildUrl($name,$params));
if(false===($xml = simplexml_load_string($data))) throw new XMLSoccerException("Invalid XML");
if(strstr($xml[0],"To avoid misuse of the service")){
switch($name){
case "GetLiveScore":
case "GetLiveScoreByLeague":
case "GetOddsByFixtureMatchID":
case "GetHistoricMatchesByLeagueAndSeason":
case "GetAllTeams":
case "GetAllTeamsByLeagueAndSeason":
throw new XMLSoccerException($xml[0],constant("self::TIMEOUT_".$name));
default:
throw new XMLSoccerException($xml[0],self::TIMEOUT_Others);
}
}
return $xml;
}
protected function buildUrl($method,$params){
$url=$this->service_url."/".$method."?apikey=".$this->api_key;
for($i=0;$i<count($params);$i++){
if(is_array($params[$i])){
foreach($params[$i] as $key=>$value){
$url.="&".strtolower($key)."=".rawurlencode($value);
}
}
else{
throw new XMLSoccerException("Arguments must be an array");
}
}
return $url;
}
protected function request($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT_CURL);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT_CURL);
curl_setopt($curl, CURLOPT_INTERFACE,$this->request_ip);
$data = curl_exec($curl);
$cerror=curl_error($curl);
$cerrno=curl_errno($curl);
$http_code=curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($cerrno!=0) throw new XMLSoccerException($cerror,E_USER_WARNING);
if($http_code == 200 ) return $data;
throw new XMLSoccerException($http_code .' - '. $data . "\nURL: " . $url);
}
public function setRequestIp($ip){
if(empty($ip)) throw new XMLSoccerException("IP parameter cannot be empty",E_USER_WARNING);
$this->request_ip=$ip;
}
public function setServiceUrl($service_url){
if(empty($service_url)) throw new XMLSoccerException("service_url parameter cannot be empty",E_USER_WARNING);
$this->service_url=$service_url;
}
}
class XMLSoccerException extends Exception{
}
try{
$soccer=new XMLSoccer("your_key");
$soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
$result=$soccer->GetLeagueStandingsBySeason(array("league"=>3,"seasonDateString"=>"1314"));
print_r($result);
}
catch(XMLSoccerException $e){
echo "XMLSoccerException: ".$e->getMessage();
}
?>
The result is this
SimpleXMLElement Object ( [TeamLeagueStanding] => Array ( [0] => SimpleXMLElement Object ( [Team] => Celtic [Team_Id] => 54 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 31 [Draw] => 6 [Lost] => 1 [NumberOfShots] => 840 [YellowCards] => 44 [RedCards] => 2 [Goals_For] => 102 [Goals_Against] => 25 [Goal_Difference] => 77 [Points] => 99 ) [1] => SimpleXMLElement Object ( [Team] => Motherwell [Team_Id] => 47 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 22 [Draw] => 4 [Lost] => 12 [NumberOfShots] => 556 [YellowCards] => 59 [RedCards] => 2 [Goals_For] => 64 [Goals_Against] => 60 [Goal_Difference] => 4 [Points] => 70 ) [2] => SimpleXMLElement Object ( [Team] => Aberdeen [Team_Id] => 45 [Played] => 38 [PlayedAtHome] => 18 [PlayedAway] => 20 [Won] => 20 [Draw] => 8 [Lost] => 10 [NumberOfShots] => 548 [YellowCards] => 50 [RedCards] => 4 [Goals_For] => 53 [Goals_Against] => 38 [Goal_Difference] => 15 [Points] => 68 ) [3] => SimpleXMLElement Object ( [Team] => Dundee United [Team_Id] => 51 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 16 [Draw] => 10 [Lost] => 12 [NumberOfShots] => 610 [YellowCards] => 49 [RedCards] => 3 [Goals_For] => 65 [Goals_Against] => 50 [Goal_Difference] => 15 [Points] => 58 ) [4] => SimpleXMLElement Object ( [Team] => Inverness C [Team_Id] => 48 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 16 [Draw] => 9 [Lost] => 13 [NumberOfShots] => 524 [YellowCards] => 55 [RedCards] => 2 [Goals_For] => 44 [Goals_Against] => 44 [Goal_Difference] => 0 [Points] => 57 ) [5] => SimpleXMLElement Object ( [Team] => St Johnstone [Team_Id] => 46 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 15 [Draw] => 8 [Lost] => 15 [NumberOfShots] => 514 [YellowCards] => 59 [RedCards] => 6 [Goals_For] => 48 [Goals_Against] => 42 [Goal_Difference] => 6 [Points] => 53 ) [6] => SimpleXMLElement Object ( [Team] => Ross County [Team_Id] => 360 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 11 [Draw] => 7 [Lost] => 20 [NumberOfShots] => 486 [YellowCards] => 65 [RedCards] => 5 [Goals_For] => 44 [Goals_Against] => 62 [Goal_Difference] => -18 [Points] => 40 ) [7] => SimpleXMLElement Object ( [Team] => St Mirren [Team_Id] => 56 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 10 [Draw] => 9 [Lost] => 19 [NumberOfShots] => 476 [YellowCards] => 47 [RedCards] => 2 [Goals_For] => 39 [Goals_Against] => 58 [Goal_Difference] => -19 [Points] => 39 ) [8] => SimpleXMLElement Object ( [Team] => Kilmarnock [Team_Id] => 52 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 11 [Draw] => 6 [Lost] => 21 [NumberOfShots] => 472 [YellowCards] => 55 [RedCards] => 5 [Goals_For] => 45 [Goals_Against] => 66 [Goal_Difference] => -21 [Points] => 39 ) [9] => SimpleXMLElement Object ( [Team] => Partick [Team_Id] => 561 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 8 [Draw] => 14 [Lost] => 16 [NumberOfShots] => 623 [YellowCards] => 67 [RedCards] => 5 [Goals_For] => 46 [Goals_Against] => 65 [Goal_Difference] => -19 [Points] => 38 ) [10] => SimpleXMLElement Object ( [Team] => Hibernian [Team_Id] => 53 [Played] => 38 [PlayedAtHome] => 20 [PlayedAway] => 18 [Won] => 8 [Draw] => 11 [Lost] => 19 [NumberOfShots] => 507 [YellowCards] => 62 [RedCards] => 5 [Goals_For] => 31 [Goals_Against] => 51 [Goal_Difference] => -20 [Points] => 35 ) [11] => SimpleXMLElement Object ( [Team] => Hearts [Team_Id] => 50 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 10 [Draw] => 8 [Lost] => 20 [NumberOfShots] => 486 [YellowCards] => 68 [RedCards] => 5 [Goals_For] => 45 [Goals_Against] => 65 [Goal_Difference] => -20 [Points] => 23 ) )