Jump to content

Create API through module


JoelWebsites

Recommended Posts

I want to insert the following below code into my module ..the below code creates an api ...How do I insert this code in prestashop.. The code is taken from a php file which has generated a api sucessfully.

 

<?php

 

//if(isset($_GET['format']) && intval($_GET['num'])) {
 
//Set our variables
$format = strtolower($_GET['format']);
$num = intval($_GET['num']);
 
$db_host = "127.0.0.1"; 
// Place the username for the MySQL database here 
$db_username = "*********1231";  
// Place the password for the MySQL database here 
$db_pass = "*(&(&*$@##$";  
// Place the name for the MySQL database here 
$db_name = "$&**^#$@"; 
 
// Run the actual connection here  
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");  
//Run our query
$result = mysql_query('SELECT * FROM ps_employee ') or die('MySQL Error.');
 
//Preapre our output
if($format == 'json') {
 
$recipes = array();
while($recipe = mysql_fetch_array($result, MYSQL_ASSOC)) {
$recipes[] = array('post'=>$recipe);
}
 
$output = json_encode(array('posts' => $recipes));
 
} elseif($format == 'xml') {
 
header('Content-type: text/xml');
$output  = "<?xml version=\"1.0\"?>\n";
$output .= "<recipes>\n";
 
for($i = 0 ; $i < mysql_num_rows($result) ; $i++){
$row = mysql_fetch_assoc($result);
$output .= "<recipe> \n";
$output .= "<recipe_id>" . $row['id_employee'] . "</recipe_id> \n";
$output .= "<recipe_name>" . $row['email'] . "</recipe_name> \n";
 
}
 
$output .= "</recipes>";
 
} else {
die('Improper response format.');
}
 
//Output the output.
echo $output;
 
//}
 
?>
Edited by joeljfernandes (see edit history)
Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...