Reputation: 161
Simple question, I should think. Unfortunately, I couldn't figure out how to get it working.
I have an xml data sheet that looks like this:
<leads>
<leadDetails>
<lead_id>3886961</lead_id>
<campaign_id>1173</campaign_id>
<campaign_name>Tesco vs Argos - Submit (UK)</campaign_name>
<subID>N/A</subID>
<gateway>yes</gateway>
<ip>109.155.249.140</ip>
<referring_url>http://adworkmedia.com/gTemplate.php?GID=1514&pubID=2312&sid=&ST=
</referring_url>
<date>2012-03-24 07:19:07</date>
<payout>$0.70</payout>
<status>Credited</status>
</leadDetails>
</leads>
I should add that this XML sheet is not hosting on my servers, but is dynamically updated as I get a new lead, by my network. Feel free to ask me more questions if needed.
MY QUESTION IS: How can I write a code that reads the XML page, and uploads, for each , the date, the IP, and the campaign_name to a mySQL table.
Thanks!
Upvotes: 1
Views: 11743
Reputation: 534
Hi.
if( $xml = simplexml_load_file("the_url_of_xml"))
{
foreach($xml->leads->leadDetails as $detail)
{
$camp_id = $detail->campaign_id;
$camp_name = $detail->campaign_name;
$sql = "INSERT INTO myTable (id,name) VALUES ('$camp_id','$camp_name')";//AND so on
mysql_query($sql);
}
}
Upvotes: 1
Reputation: 11
JSON would be a better format in my opinion, but for XML, it's pretty simple.
<?php
$con = mysql_connect("username","password","host");
mysql_select_db("database_name",$con);
$xmlObj = new SimpleXMLElement($xml); //replace $xml with your XML string.
foreach($xmlObj->leadDetails as $details){
$date = $details->date;
$ip = $details->ip;
$campaign_name = $details->campaign_name;
mysql_query("INSERT INTO `table_name` (`date`,`ip`,`campaign_name`) VALUES ('$date','$ip','$campaign_name')",$con);
}
?>
Upvotes: 1
Reputation: 1287
Just a starting point for your reference, using SimpleXML and PDO:
$xml = simplexml_load_file('http://yourhost.com/somefile.xml');
$pdo = new PDO('mysql:dbname=test;host=localhost', 'user', 'pass');
foreach ($xml->leadDetails as $lead) {
$stmt = $pdo->prepare('INSERT INTO table (createdAt, ip, campaign_name) VALUES (NOW(), ?, ?)');
$stmt->execute(array(
$_SERVER['REMOTE_ADDR'],
$lead->campaign_name
));
}
Upvotes: 1