Module Web Services API REST (developer): Difference between revisions

Jump to navigation Jump to search
m
no edit summary
Tag: 2017 source edit
mNo edit summary
Tag: 2017 source edit
Line 38: Line 38:
However, NGinx default setup on a lot of distribution is often a gaz engine and you may experience troubles. If you experience suche troubles, you can try to edit your NGinx config file to match the following example. This is a simple localhost setup with a working REST API handling (tested with 9.0.1 on parabola gnu/linux).
However, NGinx default setup on a lot of distribution is often a gaz engine and you may experience troubles. If you experience suche troubles, you can try to edit your NGinx config file to match the following example. This is a simple localhost setup with a working REST API handling (tested with 9.0.1 on parabola gnu/linux).


<source language="nginx">
<syntaxHighlight language="nginx">
worker_processes  1;
worker_processes  1;
error_log  /var/log/nginx/error.log;
error_log  /var/log/nginx/error.log;
Line 93: Line 93:
     }
     }
}
}
</source>
</syntaxHighlight>


= List of provided services =
= List of provided services =
Line 130: Line 130:




<source lang="php">
<syntaxHighlight lang="php">
// Example of function to call a REST API
// Example of function to call a REST API
function callAPI($method, $apikey, $url, $data = false)
function callAPI($method, $apikey, $url, $data = false)
Line 174: Line 174:


     return $result;
     return $result;
}</source>
}</syntaxHighlight>




Line 198: Line 198:
* $apiUrl = "<nowiki>http://<my_server>/api/index.php/</nowiki>";
* $apiUrl = "<nowiki>http://<my_server>/api/index.php/</nowiki>";


<source lang="php">
<syntaxHighlight lang="php">
// Retrieve products list
// Retrieve products list
$listProduits = [];
$listProduits = [];
Line 211: Line 211:
}
}
}
}
</source>
</syntaxHighlight>
Comments :
Comments :
* I retrieve the 10'000 first products sorted on their ID in the base
* I retrieve the 10'000 first products sorted on their ID in the base
Line 218: Line 218:




<source lang="php">
<syntaxHighlight lang="php">
// Create a product
// Create a product
$ref = "my_product_ref_X203ZZ";
$ref = "my_product_ref_X203ZZ";
Line 227: Line 227:
$newProductResult = CallAPI("POST", $apiKey, $apiUrl."products", json_encode($newProduct));
$newProductResult = CallAPI("POST", $apiKey, $apiUrl."products", json_encode($newProduct));
$newProductResult = json_decode($newProductResult, true);
$newProductResult = json_decode($newProductResult, true);
</source>
</syntaxHighlight>
Comments :
Comments :
* before creating a product, it could be wise to check if it exists. Using my first example, you'll have  :
* before creating a product, it could be wise to check if it exists. Using my first example, you'll have  :




<source lang="php">
<syntaxHighlight lang="php">
// my ref
// my ref
$ref = "my_product_ref_X203ZZ";
$ref = "my_product_ref_X203ZZ";
Line 259: Line 259:
}
}
}
}
</source>
</syntaxHighlight>
Comments :
Comments :
* I check if the ref of my product exist in the array created in the first example.
* I check if the ref of my product exist in the array created in the first example.
Line 268: Line 268:
Warning: The example below is not working properly. Creating an order and lines in one go is not supported. Instead you have to create an order first and with the returned order id add the lines using the /orders/{id}/lines endpoint.  
Warning: The example below is not working properly. Creating an order and lines in one go is not supported. Instead you have to create an order first and with the returned order id add the lines using the /orders/{id}/lines endpoint.  


<source lang="php">
<syntaxHighlight lang="php">
// create an order with 2 products
// create an order with 2 products


Line 312: Line 312:
$newCommandeResult = json_decode($newCommandeResult, true);
$newCommandeResult = json_decode($newCommandeResult, true);
}
}
</source>
</syntaxHighlight>
Comments :
Comments :
* $clientDoliId is the ID of the customer in the Dolibarr database. Either you know it or you can search for it before
* $clientDoliId is the ID of the customer in the Dolibarr database. Either you know it or you can search for it before
Line 318: Line 318:




<source lang="php">
<syntaxHighlight lang="php">
// Validate an order  
// Validate an order  
$newCommandeValider = [
$newCommandeValider = [
Line 327: Line 327:
$newCommandeValiderResult = json_decode($newCommandeValiderResult, true);
$newCommandeValiderResult = json_decode($newCommandeValiderResult, true);


</source>
</syntaxHighlight>
Comments :
Comments :
* in this example, on the penultimate line, we can see : $apiUrl."orders/".$newCommandeResult."/validate".<br/>
* in this example, on the penultimate line, we can see : $apiUrl."orders/".$newCommandeResult."/validate".<br/>
Line 333: Line 333:




<source lang="php">
<syntaxHighlight lang="php">
// search in the database if a customer exist
// search in the database if a customer exist
$customer_name = "Acme Inc";
$customer_name = "Acme Inc";
Line 345: Line 345:
)
)
), true);
), true);
</source>
</syntaxHighlight>
Comments :
Comments :
* limit => 1 only 1 customer
* limit => 1 only 1 customer
Line 352: Line 352:




<source lang="php">
<syntaxHighlight lang="php">
// customer doesn't exist. Let's create it and get it's ID
// customer doesn't exist. Let's create it and get it's ID
$newClient = [
$newClient = [
Line 363: Line 363:
$newClientResult = json_decode($newClientResult, true);
$newClientResult = json_decode($newClientResult, true);
$clientDoliId = $newClientResult;
$clientDoliId = $newClientResult;
</source>
</syntaxHighlight>
Comments :
Comments :
* client => 1 He is a customer (not a supplier)
* client => 1 He is a customer (not a supplier)

Navigation menu