-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
175 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
## Class db | ||
Class db prepare statements for mysql queries to the connected database. | ||
We use the global ``$db`` instance to access its methods. | ||
|
||
### query () | ||
Runs a query and returns the result. | ||
|
||
**Parameters** | ||
- $q:string The query. | ||
- $args:array (optional) Values to prepare the statement. | ||
|
||
Examples: | ||
``` | ||
$result1 = $db->query("SELECT title,author FROM post;"); | ||
$result2 = $db->query("SELECT title,author FROM post WHERE user_id=?;",[session::user_id()]); | ||
``` | ||
|
||
|
||
### get () | ||
Runs a query and returns the results as an array. | ||
|
||
**Parameters** | ||
- $q:string The query. | ||
- $args:array (optional) Values to prepare the statement. | ||
|
||
Example: | ||
``` | ||
$result = $db->get("SELECT title,author FROM post;"); | ||
// Returns | ||
[ | ||
0=>[0=>'Lorem ipsum', 'title'=>'Lorem ipsum', 1=>'John', 'author'=>'John'], | ||
1=>[0=>'Duis aute irure', 'title'=>'Duis aute irure', 1=>'John', 'author'=>'John'], | ||
] | ||
``` | ||
|
||
|
||
### gen () | ||
Runs a query and returns a generator that yields the rows. | ||
|
||
**Parameters** | ||
- $q:string The query. | ||
- $args:array (optional) Values to prepare the statement. | ||
|
||
Example: | ||
``` | ||
$generator = $db->gen("SELECT title,author FROM post;"); | ||
``` | ||
|
||
|
||
### getRows () | ||
Runs a query and returns the results as an array. With rows fetched with mysqli_fetch_row(). | ||
|
||
**Parameters** | ||
- $q:string The query. | ||
- $args:array (optional) Values to prepare the statement. | ||
|
||
Example: | ||
``` | ||
$result = $db->get("SELECT title,author FROM post;"); | ||
// Returns | ||
[ | ||
0=>[0=>'Lorem ipsum',1=>'John'], | ||
1=>[0=>'Duis aute irure',1=>'John'], | ||
] | ||
``` | ||
|
||
|
||
### getList () | ||
Runs a query and returns an array with the values of the first columns from the results. | ||
|
||
**Parameters** | ||
- $q:string The query. | ||
- $args:array (optional) Values to prepare the statement. | ||
|
||
Example: | ||
``` | ||
$titles = $db->get("SELECT title,author FROM post;"); | ||
// Returns | ||
[0=>'Lorem ipsum', 1=>'Duis aute irure'] | ||
``` | ||
|
||
|
||
### value () | ||
Runs a query and returns the value of the first column of the first row of the results. | ||
|
||
**Parameters** | ||
- $q:string The query. | ||
- $args:array (optional) Values to prepare the statement. | ||
|
||
Example: | ||
``` | ||
$res = $db->get("SELECT title FROM post WHERE id=1;"); | ||
// returns | ||
'Lorem ipsum' | ||
``` | ||
|
||
|
||
### error () | ||
Return an error if exists from the last query executed. | ||
|
||
Example: | ||
``` | ||
$res = $db->get("SELECT title,author FROM post;"); | ||
if ($error = $db->error()) { | ||
trigger_error($error); | ||
} | ||
``` | ||
|
||
|
||
### close () | ||
Closes the connection to the database. | ||
|
||
Example | ||
``` | ||
$db->close(); | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
### Authentication | ||
|
||
In order to make the calls of Web APIs from a different domain you will need to use the token from your user. | ||
|
||
**How to generate a Token** | ||
|
||
You can generate a unique token key from the */admin/profile* page. Keep this key in secret. | ||
|
||
|
||
**Use the Token from server calls (PHP)** | ||
|
||
You can send the token as post parameter: | ||
``` | ||
$url = "https://example.com/cm/delete/post"; | ||
$token = "<UNIQUE_TOKEN>"; | ||
$options = [ | ||
'http' => [ | ||
'method' => 'POST', | ||
'header' => "Content-type: application/json", | ||
'content' => http_build_query(['id'=>2, 'token'=>$token]), | ||
'ignore_errors' => true | ||
] | ||
]; | ||
$context = stream_context_create($options); | ||
$response = file_get_contents($url, false, $context); | ||
``` | ||
|
||
**Authenticate from front-end (Javascript)** | ||
|
||
In order to make calls from a different domain, you should include the domain of your front-end app in the website's **cors** value. In *config.php* of your installation add: | ||
``` | ||
'cors'=> ['myapp.com'] | ||
``` | ||
|
||
From javascript you should authenticate first the user with cedentials and then use the token in your calls. | ||
|
||
Example using axios: | ||
``` | ||
// authenticate | ||
axios.post('https://example.com/login/auth', { | ||
email: 'user@mail.com', | ||
password: 'password' | ||
}) | ||
.then((response) => { | ||
token_key = response.data.token; | ||
}); | ||
// send a request | ||
axios.post('https://example.com/cm/delete/post', { | ||
id: 2, | ||
token: token_key | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ Contents: | |
:maxdepth: 2 | ||
:caption: Web Api: | ||
|
||
auth | ||
cm | ||
fm | ||
|
||
|