-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.php
95 lines (89 loc) · 2.86 KB
/
services.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
$host = 'https://collaborate.thegooddata.org';
$adminLogin = 'tgdAdmin';
$adminPassword = 'tGdOA2346';
$userName = 'foo';
$userPassword ='foo';
$userEmail = '[email protected]';
$userScreenName = 'Foo';
/*
* Server REST - user.login
*/
// REST Server URL
$request_url = $host.'/rest/user/login';
// User data
$user_data = array(
'username' => /*admin login*/$adminLogin,
'password' => /*admin password*/$adminPassword,
);
$user_data = json_encode($user_data);
// cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json', "Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, FALSE); // Ask to not return Header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check if login was successful
if ($http_code == 200) {
// Convert json response as array
$logged_user = json_decode($response);
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}
// Define cookie session
$cookie_session = $logged_user->session_name . '=' . $logged_user->sessid;
//GET CSRF TOKEN
$csrf_token = $logged_user->token;
/*
* Server REST - user.create
*/
// REST Server URL
$request_url = $host.'/rest/user/register';
// User data
$user_data = array(
'name' => /*user name*/ $userName,
'mail' => /*email*/ $userEmail,
'pass' => /*password*/ $userPassword,
'status' => 1,
'field_user_display_name' => array(
'und' => array (
0 => array (
'value' => /*screen name*/ $userScreenName,
'format' => NULL,
'safe_value' => /*screen name*/ $userScreenName,
),
),
),
);
$user_data = json_encode($user_data);
// cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_POST, TRUE); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json', "Content-type: application/json", 'X-CSRF-Token: '.$csrf_token));
curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, TRUE); // Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$cookie_session"); // use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, FALSE); //True in prod, false for debugging
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check if login was successful
if ($http_code == 200) {
// Convert json response as array
$user = json_decode($response);
echo $user;
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}