Jump to content

save simplexmlelement object to mysql


vivis933

Recommended Posts

I need to save the data in mysql into rows and colums. pls some help

The colums to be like Team , Team id , played etc

<?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 ) )

 

 

Link to comment
Share on other sites

You would use a loop somehting like this to get the data for insertion into your database table

foreach ($results->TeamLeagueStanding as $team) {
    $team = $team->Team;
    $teamid = $team->Team_Id;
    $played = $team->Played;
    $playedathome = $team->PlayedAtHome;
    $playedathome = $team->PlayedAway;
    $won = $team->Won;
    $draw = $team->Draw;
    $lost = $team->Lost;
    $shots = $team->NumberOfShots;
    $yellowcards = $team->YellowCards;
    $redcards = $team->RedCards;
    $goalsfor = $team->Goals_For;
    $goalsagainst = $team->Goals_Against;
    $goaldiff = $team->Goal_Difference;
    $points = $team->Points;

    // INSERT query goes here
}

NOTE: some of those fields are redundant, namely played, goaldiff and points.

  • played = playedhome + playedaway
  • goaldiff = goalsfor - goalsagainst
  • points = ( wins * 3 ) + draws
Link to comment
Share on other sites

Thnx for the replay I changed the code and included your statement in it but it gives to me an error

<?php
include("XMLSoccer.php");
include ("conf.php");
$file_name = basename(__FILE__,'.php');
try{
$soccer=new XMLSoccer($api_key);
$soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
$result=$soccer->GetAllLeagues();
print_r($result);
}
catch(XMLSoccerException $e){
echo "XMLSoccerException: ".$e->getMessage();
}
$table = $file_name;
$query = "SELECT ID FROM " . $table;
$result = mysqli_query($conn,$query);
if(empty($result)) {
echo "<p>" . $table . " table does not exist</p>";
$query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
Id VARCHAR(300),
Name VARCHAR(300),
Country VARCHAR(30),
Historical_Data VARCHAR(300),
Fixtures VARCHAR(300),
Livescore VARCHAR(300),
NumberOfMatches VARCHAR(30),
LatestMatch VARCHAR(300),
IsCup VARCHAR(300)
)");
}
else {
echo "<p>" . $table . "table exists</p>";
} // else
foreach ($results->League as $team) {
$leagueid = $team->Id;
$name = $team->Name;
$country = $team->Country;
$historical_Data = $team->Historical_Data;
$fixtures = $team->Fixtures;
$livescore = $team->Livescore;
$numberOfMatches = $team->NumberOfMatches;
$latestMatch = $team->LatestMatch;
$isCup = $team->IsCup;
}
?>

THE ERROR 

 

 

 


 
<p>GetAllLeagues2table exists</p>
Notice: Undefined variable: results in C:\...\GetAllLeagues2.php on line 34
PHP Notice:  Undefined variable: results in C:\...\GetAllLeagues2.php on line 34
PHP Notice:  Trying to get property of non-object in C:\...\GetAllLeagues2.php on line 34
PHP Warning:  Invalid argument supplied for foreach() in C:\...\GetAllLeagues2.php on line 34
 
Notice: Trying to get property of non-object in C:\...\GetAllLeagues2.php on line 34
 
Warning: Invalid argument supplied for foreach() in C:\...\GetAllLeagues2.php on line 34
 
Process finished with exit code 0
 

 

 

Edited by vivis933
Link to comment
Share on other sites

oh i didn't see that :happy-04: but now i get this error

 

 
<p>GetAllLeagues2table exists</p>
Notice: Undefined property: mysqli_result::$League in C:\...\GetAllLeagues2.php on line 34
 
Warning: Invalid argument supplied for foreach() in C:\...\GetAllLeagues2.php on line 34
PHP Notice:  Undefined property: mysqli_result::$League in C:\...\GetAllLeagues2.php on line 34
PHP Warning:  Invalid argument supplied for foreach() in C:\...\GetAllLeagues2.php on line 34
 

Process finished with exit code 0 

 

Link to comment
Share on other sites

 

<?php

include("XMLSoccer.php");

include ("conf.php");

$file_name = basename(__FILE__,'.php');

try{

$soccer=new XMLSoccer($api_key);

$soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");

$result=$soccer->GetAllLeagues();

print_r($result);

}

catch(XMLSoccerException $e){

echo "XMLSoccerException: ".$e->getMessage();

}

$table = $file_name;

$query = "SELECT ID FROM " . $table;

$result = mysqli_query($conn,$query);

 

Whatever was in the first $result variable has now neen overwritten later on.

 

Also, your original (posted) result output had no League property, but it did have TeamLeagueStanding

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.