Jump to content

For those who want to Create your their own custom api json and xml


JoelWebsites

Recommended Posts

Below is the code to create your own api using prestashop database.

webservice.php

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
//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 = "username";  
// Place the password for the MySQL database here 
$db_pass = "password";  
// Place the name for the MySQL database here 
$db_name = "prestashop"; 
 
// 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 .= "<recipes> \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;
 
//}
 
?>
 
 
 
 
 
Below is how to retrive 
 
retrive.php
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
 
$content = file_get_contents($url);
$json = json_decode($content, true);
 
print_r($json);
 
 
echo "Name: ".$json[posts][1][firstname]."<br>"; //getting the username

 

  • Like 2
Link to comment
Share on other sites

  • 10 months later...

Hi,

 

I am relatively new in php and module creation, I am c# developper.

 

I wonder if I can andapt you code to retrieve data from a web service describe in https://platform.medipim.be/docs/api/v1.html.

 

The module must do three things:

- update products, like price, picture

- create new products based on filter like brand or reference.

- Map some fields like category

 

Is this very difficult to do ?

 

Jean-Marie

  • Like 1
Link to comment
Share on other sites

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