Xml to sql database - php

iam trying to make an php file to import xml to sql database, but i have this error-:
Warning: Invalid argument supplied for foreach() in /home/morrisse/public_html/xml_to_database.php on line 18
My code:
$url ="http://www.example/example.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL); //GET THE URL CONTENDS
$data = curl_exec ($ch); //execute the curl request
curl_close($ch);
$xml = simplexml_load_string($data);
foreach ($xml -> track as $row) {
$title = $row -> name;
$artist = $row -> artist;
$duration = $row -> duration;
// performing sql query
$sql = "INSERT INTO 'test_xml' ('title', 'artist', 'duration')"
. "VALUES ('$title', '$artist', '$duration')";
$result = mysql_query($sql);
if (!result) {
echo 'MYSQL ERROR';
} else {
echo 'SUCESSO';
}
}
my xml:
<lfm status="ok">
<track>
<id>602366984</id>
<name>Barbarism Begins At Home (BBC TV Tube performance)</name>
<mbid/>
<url>http://www.last.fm/music/The+Smiths/_/Barbarism+Begins+At+Home+(BBC+TV+Tube+performance)</url>
<duration>384000</duration>
<streamable fulltrack="0">0</streamable>
<listeners>10</listeners>
<playcount>24</playcount>
<artist>
<name>The Smiths</name>
<mbid>40f5d9e4-2de7-4f2d-ad41-e31a9a9fea27</mbid>
<url>http://www.last.fm/music/The+Smiths</url>
</artist>
<toptags>
</toptags>
</track>
</lfm>
help please?
The full code:
<?php
$url ="http://www.morrisseyradio.com/shoutcastinfo/cache/track.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL); //GET THE URL CONTENDS
$data = curl_exec ($ch); //execute the curl request
curl_close($ch);
$con=mysql_connect("localhost", "", ""); //connect to database
mysql_select_db("morrisse_forum", $con)
if (!$con) {
die('Could not connect: ' . mysql_error());
}; // select database
libxml_use_internal_errors(true);
$xml = simplexml_load_string($data);
if ($xml === false) {
$errors = libxml_get_errors();
foreach ($errors as $error) {
echo "XML Error at line $error->line: $error->message\n";
}
}
else {
foreach ($xml -> track as $row) {
$title = $row -> name;
$artist = $row -> artist;
$duration = $row -> duration;
// performing sql query
$sql = "INSERT INTO 'test_xml' ('title', 'artist', 'duration')"
. "VALUES ('$title', '$artist', '$duration')";
$result = mysql_query($sql);
if (!result) {
echo 'MYSQL ERROR';
} else {
echo 'SUCESSO';
}
}
}
?>

For anyone the answer is:
First thing:
in the line:
curl_setopt($ch, CURLOPT_URL, $URL);
the "$URL" must be Lower
Second:
In this line:
$sql = "INSERT INTO 'test_xml' ('title', 'artist', 'duration')"
the "'" must be "`"

It's pretty clear that your call to simplexml_load_string() failed because the xml was not well formed. When that happens you get back false rather than a usable xml element.
Here's what I suggest you do about this. Add some error-tracking code, as follows.
libxml_use_internal_errors(true);
$xml = simplexml_load_string($data);
if ($xml === false) {
$errors = libxml_get_errors();
foreach ($errors as $error) {
echo "XML Error at line $error->line: $error->message\n";
}
}
else {
foreach ( $xml->track as $row ) { /* your code continues here ... */
There's clearly something wrong with the xml you're getting back from that server you're using with curl. This code will help you figure out what is wrong.

Related

Exlpode xml and turn into array

I want to know how to explode or split xml and turn into array and then insert it to database. Because i have an api that need to hit every day and it return xml.
here's my xml sample:
<ArrayOfUnitPrice>
<UnitPrice>
<PriceAmount>1579.7080</PriceAmount>
<PriceDate>2016-09-02</PriceDate>
<PriceType>XWZ</PriceType>
</UnitPrice>
<UnitPrice>
<PriceAmount>1028.4137</PriceAmount>
<PriceDate>2016-09-02</PriceDate>
<PriceType>ABC</PriceType>
</UnitPrice>
...
</ArrayOfUnitPrice>
I'm using this code to extract the xml response:
$ch = curl_init("111.222.333.444:8080/code.asmx/Price");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
//my code here
curl_close($ch);
SOLVED
I already done solve my code by using this code below guys.
$xml = simplexml_load_string($result);
for ($i=0; $i < count($xml) ; $i++) {
$arr[] = array (
'PriceAmount' => $xml->UnitPrice[$i]->PriceAmount,
'PriceDate' => $xml->UnitPrice[$i]->PriceDate,
'PriceType' => $xml->UnitPrice[$i]->PriceType
);
}
$data = json_decode(json_encode($arr), true);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dailywork";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(is_array($data)){
$check = "SELECT * FROM table_unit";
if($conn->query($check)->num_rows > 0){
// my stuff to here :-D
}else{
/*Insert data to DB*/
$sql = "INSERT INTO table_unit (PriceAmount, PriceDate, PriceType) values ";
$valuesArr = array();
foreach($data as $row){
$PriceAmount = $row[PriceAmount][0];
$PriceDate = $row[PriceDate][0];
$PriceType = $row[PriceType][0];
$valuesArr[] = "('$PriceAmount', '$PriceDate', '$PriceType')";
}
$sql .= implode(',', $valuesArr);
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
/*End Insert data to DB*/
}
}
$conn->close();
This function will give you an array of your xml
function xml2php($xmlcontent)
{
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $xmlcontent, $arr_vals);
xml_parser_free($xml_parser);
return $arr_vals;
}
pass your $result like this and check it
xml2php($result);
let me know if it helps you

Why does my table stay blank even though it echos Success?

Hello there I've written some PHP that gets XML from a website, and will store it on a table on my database. It echo's Success, yet the table is blank? Heres my code:
<?php
$url ="http://rates.fxcm.com/RatesXML3";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec ($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
$con = mysql_connect("localhost","username","password"); //this does contain proper information, just ive hidden it with the following
mysql_select_db("database", $con) or die(mysql_error()); //same for database too
foreach ($xml -> Rate as $row){
$Symbol = $row -> Symbol;
$Bid = $row -> Bid;
$Ask = $row -> Ask;
//performing sql query
$sql = "INSERT INTO 'FXCM_Rates' ('Symbol', 'Bid', 'Ask')"
. "VALUES ('$Symbol', '$Bid', '$Ask')";
$result = mysql_query($sql);
if (!result) {
echo 'MySQL ERROR';
} else{
echo 'SUCCESS';
}
}
?>
Now when I check the PHP, it has echoed success for all 63 Rates, yet when I go to check the table, it is blank? I just find it odd that it has worked for everyone else, but not me :(
Thanks to anyone that can help fix my code / point out my error.
You have '$' missing in the end.
if (!$result) {
Also now that I've done some tests, your variables are objects which I'm not sure that you can INSERT in database like that.
foreach ($xml -> Rate as $row){
$Symbol = (string)$row -> Symbol;
$Bid = (string)$row -> Bid;
$Ask = (string)$row -> Ask;
}
This will return strings that might be easier to use for you query
mysql is deprecated in newer version of php, you should be using mysqli(improved)..
<?php
$url ="http://rates.fxcm.com/RatesXML3";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec ($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
$con = mysqli_connect("localhost","username","password","database") or die(mysqli_error($con));
foreach ($xml -> Rate as $row){
$Symbol = $row -> Symbol;
$Bid = $row -> Bid;
$Ask = $row -> Ask;
//performing sql query
$sql = "INSERT INTO 'FXCM_Rates' ('Symbol', 'Bid', 'Ask')"
. "VALUES ('$Symbol', '$Bid', '$Ask')";
$result = mysqli_query($con,$sql);
if (!$result) {
echo 'MySQL ERROR';
} else{
echo 'SUCCESS';
}
}

SQL Query in PHP script doesnt fill table in database

In PHP script I parsed some .csv file and trying to execute on a few ways and this is a closest I can get. When I run query manually in database everything is o.k but when I go through the the script I just got New record created successfully and the table stays empty except ID which count how many inserts I got.
o.k that's cool optimization but I still don't getting the data. Yap the $dataPacked is clear below is my whole script can you pls gave some suggestion.
<?php
class AdformAPI {
private $baseUrl = 'https://api.example.com/Services';
private $loginUrl = '/Security/Login';
private $getDataExportUrl = '/DataExport/DataExportResult?DataExportName=ApiTest';
public function login($username, $password) {
$url = $this->baseUrl . $this->loginUrl;
$params = json_encode(array('UserName' => $username, 'Password' => $password));
$response = $this->_makePOSTRequest($url, $params);
$response = json_decode($response, true);
if (empty($response['Ticket'])) {
throw new \Exception('Invalid response');
}
// var_dump($response);
return $response['Ticket'];
}
public function getExportData($ticket) {
$url = $this->baseUrl . $this->getDataExportUrl;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Ticket: '. $ticket
));
$output = curl_exec($ch);
return $output;
}
public function downloadFileFromUrl($url, $savePath) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
$data = curl_exec ($ch);
$error = curl_error($ch);
curl_close ($ch);
// if (!is_dir($savePath) && is_writable($savePath)) {
$file = fopen($savePath, "w+");
fputs($file, $data);
fclose($file);
// } else {
// throw new \Exception('Unable to save file');
// }
}
private function _makePOSTRequest($url, $json_data) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($json_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}
// Login and data download url
$api = new AdformAPI();
$ticket = $api->login('example', '123546');
$exportDataResponseJson = $api->getExportData($ticket);
$exportDataResponse = json_decode($exportDataResponseJson, true);
if (empty($exportDataResponse['DataExportResult']) || $exportDataResponse['DataExportResult']['DataExportStatus'] != "Done") {
throw new \Exception('GetDataExport invalid response');
}
// Download zip
$fileDir = '/var/www/html/app-catalogue/web/export';
$fileName = 'report.zip';
$filePath = $fileDir . DIRECTORY_SEPARATOR . $fileName;
$api->downloadFileFromUrl($exportDataResponse['DataExportResult']['DataExportResultUrl'], $filePath);
// Unzip
$zip = new ZipArchive;
$res = $zip->open($filePath);
$csvFilename = '';
if ($res === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$csvFilename = $zip->getNameIndex($i);
}
$zip->extractTo($fileDir);
$zip->close();
} else {
throw new Exception("Unable to unzip file");
}
// Parse CSV
$csvPath = $fileDir . DIRECTORY_SEPARATOR . $csvFilename;
if (is_readable($csvPath)) {
$dataCsv = file_get_contents($fileDir . DIRECTORY_SEPARATOR . $csvFilename);
$dataArr = explode("\n", $dataCsv);
$dataPacked = array();
foreach ($dataArr as $row) {
$row = str_replace(" ", "", $row);
//$row = wordwrap($row, 20, "\n", true);
$row = preg_replace('/^.{20,}?\b/s', "$0&nbsp", $row);
$row = explode("\t", $row);
$dataPacked[] = $row;
}
}
// SQL Connestion
$servername = "192.168.240.22";
$username = "liferaypublic";
$password = "liferaypublic";
$dbname = "liferay_dev";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->query("set names 'utf8'");
$sql = " INSERT INTO ho_adform_reports (`Timestamp`, `Campaign`, `Paid_Keywords`, `Natural_Search_Keywords`, `Referrer_Type`, `Referrer`, `Page`, `Order_ID`)
VALUES ";
$flag = true;
foreach($dataPacked as $rowArray) {
if($flag or count($rowArray)<= 7) { $flag = false; continue; }
$sql .= "('".implode("','", $rowArray)."'),";
}
$sql = trim($sql,",");
echo $sql; //For debug only
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
//var_dump($dataPacked);
try this - I am assuming you have sanitised the values in $dataPacked.
Note edited to addslashes just in case.
$conn->query("set names 'utf8'");
$sql = " INSERT INTO ho_adform_reports (`Timestamp`, `Campaign`, `Paid_Keywords`, `Natural_Search_Keywords`, `Referrer_Type`, `Referrer`, `Page`, `Order_ID`)
VALUES ";
$flag = true;
foreach($dataPacked as $rowArray) {
if($flag or count($rowArray)<= 7) { $flag = false; continue;}
foreach ($rowArray as $k=>$v) {
$sanitised = preg_replace("/[^[:alnum:][:space:]]/ui", '', $v);
$rowArray[$k] = addslashes(trim($sanitised));
}
$sql .= "('".implode("','", $rowArray)."'),";
}
$sql = trim($sql,",");
echo $sql; //For debug only
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();

Make SOAP request and insert duplicate values on database

I make a soap request in php, and I get the response from request and insert the values of the response in my database. I use my browser to run the script but something strange happens when I copy the link to another tab and run it, because when I check my database the values are duplicate. Sometimes when run the script the values are not duplicate in my database at the moment, but the next day the values are duplicate, and I don't understand why.
soap_request.php
<?php
ini_set('default_charset','utf8');
$init_today = date("Y-m-d");
$xml = 'soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:def="http://****/definitions" xmlns:cul="http:/***/****/***">
<soapenv:Header>'....;
$soapUrl = "https://****/Culture"; // asmx URL of WSDL
$headers = array(
...
"SOAPAction: http://****/****/****/*****",
"Content-length: ".strlen($xml),
);
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
...
$response = curl_exec($ch);
curl_close($ch);
$string = str_replace(array('ns1:', 'ns2:', 'ns3:'), array('', '', ''), $response);
$content = new SimpleXMLElement($string);
$con = mysqli_connect("localhost","root","***","*****");
function insertEventId($con, $content){
$array_id = array();
foreach($content->xpath('//Event') as $header){
$result = ($header->xpath('Id'));
$event_id = (string) $result[0];
$sql = "INSERT INTO eventos_sapo (event_id) VALUES ('$event_id')";
if (mysqli_query($con, $sql)) {
echo $sql . "</br>";
//echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
array_push($array_id, $event_id);
}
return $array_id;
}
function insertTitulo($con, $content, $array_id){
$count = 0;
while($count < sizeof($array_id)){
foreach($array_id as $id){
$count_aux = 0;
foreach($content->xpath('//Event') as $header){
$result = ($header->xpath('Name')); // Should output 'something'.
if($result){
$titulo = (string) $result[0];
if($count == $count_aux){
$sql = "Update eventos_sapo set titulo='$titulo' where event_id='$id';";
mysqli_set_charset($con,"utf8");
if (mysqli_query($con, $sql)) {
echo $sql . "</br>";
//echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
}
$count_aux++;
}
$count++;
}
}
}
$array_id = insertEventId($con, $content);
insertTitulo($con, $content, $array_id);
Response of request:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://***/**/***" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://***.****.**/***/**" xmlns:ns3="http://**.**.**/**/**">
<SOAP-ENV:Body>
<ns1:GetAgendaV2Response>
<ns1:GetAgendaV2Result>
<ns1:Events>
<ns1:Event>
<ns1:Id>94343</ns1:Id>
<ns1:Name>Mexa-se Mais 2011 - Orientação no Jamor (BTT e Caminhada)</ns1:Name>
<ns1:Genre>
<ns1:Id>10</ns1:Id>
<ns1:Name>Outdoor</ns1:Name>
</ns1:Genre>
</ns1:Event>
<ns1:Event>
<ns1:Id>122611</ns1:Id>
<ns1:Name>«Memória da Politécnica: Quatro Séculos de Educação, Ciência e Cultura»</ns1:Name>
<ns1:Genre>
<ns1:Id>6</ns1:Id>
<ns1:Name>Exposições</ns1:Name>
</ns1:Genre>
</ns1:Event>
....

php script duplicate image into the database

i have php script it keep downloading feeds from the internet.
but unfortunately it keep duplicate the image feed into the database.
part from the image, the script working fine on other feed like description,link, title.
php script
<?php
require 'database.php';
$url = "http://www.albaldnews.com/rss.php?cat=24";
$rss = simplexml_load_file($url);
if($rss)
{
echo '<h1>'.$rss->channel->title.'</h1>';
echo '<li>'.$rss->channel->pubDate.'</li>';
$items = $rss->channel->item;
foreach($items as $item)
{
$enclosure = $item->enclosure[0]['url'];
$ch = curl_init ("$enclosure");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$img=mysqli_real_escape_string($conn,$rawdata);
$query = "SELECT image from feedtable where link = '$img'";
$result= mysqli_query($conn, $query);
$num_rows = mysqli_num_rows($result);
if ($num_rows == 0)
{
$query1 = "INSERT INTO feedtable (image)VALUES ('$img')";
$result1= mysqli_query($conn, $query1);
echo "image added\n";
}
else
{
echo "duplicate entry\n";
}
}
}
?>

Categories