-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_client.php
48 lines (39 loc) · 1.28 KB
/
oauth_client.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
<?php
require 'api/lib/oauth/OAuth.php';
require 'api/includes/config.php';
$params = array();
foreach ($_POST as $key => $value)
$params[$key] = $value;
$login = $params['login'];
// Get api key from db
global $db, $config;
$query = "
SELECT u.api_key
FROM {$config->tables['users']} u
WHERE u.login = :login
";
$stmt = $db->prepare($query);
$stmt->bindParam(':login',$login);
$stmt->execute();
$result = $stmt->fetchColumn();
$key = $login;
$secret = $result;
$consumer = new OAuthConsumer($key, $secret);
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$url = 'http://'.$_SERVER['SERVER_NAME'].':8888/api/posts';
//$url = 'api/posts';
//$url = 'http://'.$_SERVER['SERVER_NAME'].':8888/api/posts/id/1';
//$params['_METHOD'] = 'DELETE';
//use oauth lib to sign request
$req = OAuthRequest::from_consumer_and_token($consumer, null, 'POST', $url, $params);
$req->sign_request($sig_method, $consumer, null);//note: double entry of token
//get data using signed url
$ch = curl_init($req->to_url());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200)
header('Content-type: application/json');
print_r($response);
curl_close($ch);