Wednesday, April 21, 2010

xml ,csv and Json api using php

This code helps you to create Json,CSV,Xml Api using php programing Language


include("head.php"); //for header desgn***********************
?>
if(isset($_GET['submit']))
{
$url=$_GET['url'];
//echo $url;

list($scheme,$remain1)=explode("://",$url);
list($domain,$remain2)=explode("/",$remain1);
list($file,$remain3)=explode("?",$remain2);
list($database_name,$table_name,$method_name,$format_name)=explode("&",$remain3);
list($db,$database)=explode("=",$database_name);
list($tb,$table)=explode("=",$table_name);
list($mb,$method)=explode("=",$method_name);
list($fb,$format)=explode("=",$format_name);
}


if (!@mysql_connect('localhost','root',''))
{
$results = Array(
'head' => Array(
'status' => '0',
'error_number' => '500',
'error_message' => 'Temporary Error.'.
'Our server might be down, please try again later.'
),
'body' => Array ()
);
$errors=1;
}
if (!@mysql_select_db($database)) {
$results = Array(
'head' => Array(
'status' => '0',
'error_number' => '500',
'error_message' => 'Temporary Error.'.
'Our server might be down, please try again later.'
),
'body' => Array ()
);
$errors=1;
}




switch ($format)
{
case 'xml' :
// Need a class to transform the output in XML

function convertToXML($xml_string)
{
global $file;
$filename=trim($file).".xml";

$fp=fopen($filename,"w");

fwrite($fp,$xml_string);
fclose($fp);
}
$file = 'xmlConvert';
$query1="SELECT * FROM $table";

// echo "query".$query1;
$dbresult = mysql_query($query1);
$doc = new DomDocument('1.0');
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
while($row = mysql_fetch_assoc($dbresult))
{
$occ = $doc->createElement($table);
$occ = $root->appendChild($occ);

foreach ($row as $fieldname => $fieldvalue)
{
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
}

$xml_string = $doc->saveXML();
convertToXML($xml_string);
?>
break;
case 'json' :

$values_query = "SELECT * FROM $table";
$result = mysql_query( $values_query) or die('Query failed: ' . mysql_error());
$all_recs = array();
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
$all_recs[] = $line;
}

echo json_encode($all_recs);

mysql_free_result($result);

break;




case 'csv' :
//Will serialize the output and then just echo it


function convertToCSV($csv_field)
{
global $file;
$filename=trim($file).".csv";

$fp=fopen($filename,"w");

fwrite($fp,$csv_field);
fclose($fp);
}
$file = 'csvConvert';

// echo $table1.$table2;
$sql="SHOW COLUMNS FROM $table";
$result=mysql_query($sql);

while($row=mysql_fetch_object($result))
{

$csv_field .= $row->Field.","; //$csv_field.=$row[0].",";
$cols++;
}


$csv_field.= "\n";

$values_query = mysql_query("SELECT * FROM $table");

while ($value_row = mysql_fetch_row($values_query))
{
for ($i=0;$i<$cols;$i++)
{
$csv_field.= $value_row[$i].", ";
}
$csv_field.= "\n";
}

mysql_free_result($values_query);
convertToCSV($csv_field);
?>
break;


case 'xls' :
// Will serialize the output and then just echo it

function convertToXLS($xls_field)
{
global $file;
$filename=trim($file).".xls";

$fp=fopen($filename,"w");

fwrite($fp,$xls_field);
fclose($fp);
}
$file = 'xlsConvert';

// echo $table1.$table2;
$sql="SHOW COLUMNS FROM $table";

$result=mysql_query($sql);

while($row=mysql_fetch_object($result))
{

$xls_field .= $row->Field."\t"; //$csv_field.=$row[0].",";
$cols++;
}

$xls_field.= "\n";

$values_query = mysql_query("SELECT * FROM $table");

while ($value_row = mysql_fetch_row($values_query))
{
for ($i=0;$i<$cols;$i++)
{
$xls_field.= $value_row[$i]."\t ";
}
$xls_field.= "\n";
}

mysql_free_result($values_query);
convertToXLS($xls_field);
?>

break;
}

?>
include("footer.php"); ***************for footer design ************
?>







the user should send Url as folling formate from home page





Enter URL:





 



No comments:

Post a Comment