Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial import.

  • Loading branch information...
commit 05d33bf9c55a006b2fed9c293278bff7493a5027 0 parents
Anton Lindqvist authored

Showing 2 changed files with 162 additions and 0 deletions. Show diff stats Hide diff stats

  1. +130 0 oauth2.php
  2. +32 0 readme.textile
130 oauth2.php
... ... @@ -0,0 +1,130 @@
  1 +<?php
  2 +class Oauth2 {
  3 +
  4 + public $body;
  5 + public $code;
  6 + public $accessToken;
  7 + public $clientId;
  8 + public $clientSecret;
  9 + public $options;
  10 +
  11 + function __construct($clientId, $clientSecret, $options = array()) {
  12 + $this->clientId = $clientId;
  13 + $this->clientSecret = $clientSecret;
  14 + $this->options = $options;
  15 + }
  16 +
  17 + /**
  18 + * Returns a url used for authorization.
  19 + *
  20 + * @param array $params optional query string parameters
  21 + * @return string
  22 + */
  23 + function authorizeUrl($params = array()) {
  24 + $params['client_id'] = $this->clientId;
  25 +
  26 + return $this->_buildUrl($this->options['authorize_path'], $params);
  27 + }
  28 +
  29 + function accessTokenUrl($params = array()) {
  30 + $params['client_id'] = $this->clientId;
  31 + $params['client_secret'] = $this->clientSecret;
  32 +
  33 + return $this->_buildUrl($this->options['access_token_path'], $params);
  34 + }
  35 +
  36 + function accessToken($code, $params = array()) {
  37 + $params = array_merge(array('code' => $code), $params);
  38 + $url = $this->accessTokenUrl($params);
  39 + $response = $this->_request($url);
  40 +
  41 + parse_str($response, $params);
  42 +
  43 + if (isset($params['access_token'])) {
  44 + $this->accessToken = $params['access_token'];
  45 +
  46 + return $this->accessToken;
  47 + } else {
  48 + return false;
  49 + }
  50 + }
  51 +
  52 + function setAccessToken($accessToken) {
  53 + $this->accessToken = $accessToken;
  54 +
  55 + return $this;
  56 + }
  57 +
  58 + function get($path, $params = array()) {
  59 + $params['access_token'] = $this->accessToken;
  60 + $url = $this->_buildUrl($path, $params);
  61 +
  62 + return $this->_request($url);
  63 + }
  64 +
  65 + function post($path, $postFields = array()) {
  66 + $postFields['access_token'] = $this->accessToken;
  67 + $url = $this->_buildUrl($path);
  68 + $options = array(
  69 + CURLOPT_POST => true,
  70 + CURLOPT_POSTFIELDS => $postFields
  71 + );
  72 +
  73 + return $this->_request($url, $options);
  74 + }
  75 +
  76 + function put($path, $postFields) {
  77 + $postFields['access_token'] = $this->accessToken;
  78 + $url = $this->_buildUrl($path);
  79 + $options = array(
  80 + CURLOPT_CUSTOMREQUEST => 'PUT',
  81 + CURLOPT_POSTFIELDS => $postFields
  82 + );
  83 +
  84 + return $this->_request($url, $options);
  85 + }
  86 +
  87 + function delete($path, $params = array()) {
  88 + $params['access_token'] = $this->accessToken;
  89 + $url = $this->_buildUrl($path, $params);
  90 + $options = array(
  91 + CURLOPT_CUSTOMREQUEST => 'DELETE'
  92 + );
  93 +
  94 + return $this->_request($url, $options);
  95 + }
  96 +
  97 + private function _request($url, $options = null) {
  98 + $ch = curl_init();
  99 + $default_options = array(
  100 + CURLOPT_URL => $url,
  101 + CURLOPT_HEADER => false,
  102 + CURLOPT_RETURNTRANSFER => true
  103 + );
  104 + $options = (isset($options))
  105 + ? array_merge($default_options, $options)
  106 + : $default_options;
  107 +
  108 + var_dump($options);
  109 +
  110 + curl_setopt_array($ch, $options);
  111 +
  112 + $this->body = curl_exec($ch);
  113 + $this->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  114 +
  115 + curl_close($ch);
  116 +
  117 + return ($this->code == 200) ? $this->body : false;
  118 + }
  119 +
  120 + private function _buildUrl($path, $params = null) {
  121 + $url = $this->options['site'];
  122 + $url .= (preg_match('/\/$/', $url)) ? '' : '/';
  123 + $url .= $path;
  124 + $url .= (isset($params)) ? '?' . http_build_query($params) : '';
  125 +
  126 + return $url;
  127 + }
  128 +
  129 +}
  130 +?>
32 readme.textile
Source Rendered
... ... @@ -0,0 +1,32 @@
  1 +<pre>
  2 +<code>
  3 +<?php
  4 +$oauth2 = new Oauth2(
  5 + 'client_id',
  6 + 'client_secret',
  7 + array(
  8 + 'site' => 'https://graph.facebook.com',
  9 + 'authorize_path' => 'oauth/authorize',
  10 + 'access_token_path' => 'oauth/access_token',
  11 + )
  12 +);
  13 +
  14 +if (isset($_GET['code'])) {
  15 + $accessToken = $oauth2->accessToken(
  16 + $_GET['code'],
  17 + array(
  18 + 'redirect_uri' => 'connect_url'
  19 + )
  20 + );
  21 + $feed = $oauth2->get('me/feed');
  22 +} else {
  23 + $loginUrl = $oauth2->authorizeUrl(
  24 + array(
  25 + 'scope' => 'publish_stream',
  26 + 'redirect_uri' => 'connect_url'
  27 + )
  28 + );
  29 +}
  30 +?>
  31 +</code>
  32 +</pre>

0 comments on commit 05d33bf

Please sign in to comment.
Something went wrong with that request. Please try again.