Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
piphone
piphone-front
Commits
388467ba
Commit
388467ba
authored
Aug 17, 2016
by
okhin
Browse files
Using Guzzle instead of cURL for requesting the API
parent
47668ba6
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/Api.php
View file @
388467ba
<?php
use
GuzzleHttp\Client
;
/*
* Static class for REST API communication
*/
class
Api
{
// Generic get function for REST API
static
function
get
(
$url
)
{
$c
url
=
curl_init
(
);
$c
lient
=
new
Client
([
'base_uri'
=>
API_BASE
,
'defaults'
=>
[
'headers'
=>
[
'Content-type'
=>
'applications/json'
]]]
);
$url
=
API_BASE
.
$url
.
"/?format=json"
;
curl_setopt
(
$curl
,
CURLOPT_URL
,
$url
);
curl_setopt
(
$curl
,
CURLOPT_HTTPHEADER
,
array
(
'Content-type: application/json'
));
curl_setopt
(
$curl
,
CURLOPT_RETURNTRANSFER
,
true
);
curl_setopt
(
$curl
,
CURLOPT_FOLLOWLOCATION
,
1
);
$result
=
curl_exec
(
$curl
);
curl_close
(
$curl
);
return
$result
;
$result
=
$client
->
request
(
'GET'
,
$url
);
return
$result
->
getBody
();
}
// Generic post function for REST API
static
function
post
(
$url
,
$data
)
{
$curl
=
curl_init
();
$url
=
API_BASE
.
$url
;
curl_setopt
(
$curl
,
CURLOPT_URL
,
$url
);
curl_setopt
(
$curl
,
CURLOPT_POST
,
true
);
curl_setopt
(
$curl
,
CURLOPT_POSTFIELDS
,
$data
);
curl_setopt
(
$curl
,
CURLOPT_RETURNTRANSFER
,
true
);
$result
=
curl_exec
(
$curl
);
curl_close
(
$curl
);
return
$result
;
$client
=
new
Client
([
'base_uri'
=>
API_BASE
,
'defaults'
=>
[
'headers'
=>
[
'Content-type'
=>
'applications/json'
]]]);
$url
=
API_BASE
.
$url
.
"/?format=json"
;
$result
=
$client
->
request
(
'POST'
,
$url
,
array
(),
$data
);
return
$result
->
getBody
();
}
// Asking for campaign informations
...
...
app/config.php.sample
View file @
388467ba
...
...
@@ -5,6 +5,7 @@ define("API_BASE","http://localhost/restapi/");
define
(
"API_TIMEOUT"
,
"1"
);
define
(
"JWT_KEY"
,
"YOUR_KEY"
);
define
(
"JWT_TOKEN"
,
"YOUR_TOKEN"
);
define
(
"SIP_API"
,
"http://backendsip/api"
);
/* TODO : remove this in production ;) */
error_reporting
(
E_ALL
|
E_WARNING
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment