<?php

require '../src/Curl/Curl.php';
use ZzhhCurl\Curl;
// curl \
//     -X POST \
//     -d "{"id":"1","content":"Hello world!","date":"2015-06-30 19:42:21"}" \
//     "https://httpbin.org/post"
$data = json_encode(array('id' => '1', 'content' => 'Hello world!', 'date' => date('Y-m-d H:i:s')));
$curl = new Curl();
$curl->setHeader('Content-Type', 'application/json');
$curl->post('https://httpbin.org/post', $data);
var_dump($curl->response->json);
<?php

require '../src/Curl/Curl.php';
use ZzhhCurl\Curl;
define('API_KEY', '');
define('API_SECRET', '');
$url = 'https://coinbase.com/api/v1/account/balance';
$nonce = (int) (microtime(true) * 1000000.0);
$message = $nonce . $url;
$signature = hash_hmac('sha256', $message, API_SECRET);
$curl = new Curl();
$curl->setHeader('ACCESS_KEY', API_KEY);
$curl->setHeader('ACCESS_SIGNATURE', $signature);
$curl->setHeader('ACCESS_NONCE', $nonce);
$curl->get($url);
echo 'My current account balance at Coinbase is ' . $curl->response->amount . ' ' . $curl->response->currency . '.' . "\n";
define('CLIENT_SECRET', 'XXXXXXXXXXXXXXXXXXXXXXXX');
session_start();
if (isset($_GET['code'])) {
    $code = $_GET['code'];
    // Exchange the authorization code for an access token.
    $curl = new Curl();
    $curl->post('https://accounts.google.com/o/oauth2/token', array('code' => $code, 'client_id' => CLIENT_ID, 'client_secret' => CLIENT_SECRET, 'redirect_uri' => implode('', array(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http', '://', $_SERVER['SERVER_NAME'], $_SERVER['SCRIPT_NAME'])), 'grant_type' => 'authorization_code'));
    if ($curl->error) {
        echo $curl->response->error . ': ' . $curl->response->error_description;
        exit;
    }
    $_SESSION['access_token'] = $curl->response->access_token;
    header('Location: ?');
} elseif (!empty($_SESSION['access_token']) && !isset($_GET['retry'])) {
    // Use the access token to retrieve the profile.
    $curl = new Curl();
    $curl->setHeader('Content-Type', 'application/json');
    $curl->setHeader('Authorization', 'OAuth ' . $_SESSION['access_token']);
    $curl->get('https://www.googleapis.com/plus/v1/people/me');
    if ($curl->error) {
        echo 'Error ' . $curl->response->error->code . ': ' . $curl->response->error->message . '.<br />';
        echo '<a href="?retry">Retry?</a>';
        exit;
    }
    echo 'Hi ' . $curl->response->displayName . '.';
} else {
    $curl = new Curl();
    $curl->get('https://accounts.google.com/o/oauth2/auth', array('scope' => 'https://www.googleapis.com/auth/plus.me', 'redirect_uri' => implode('', array(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http', '://', $_SERVER['SERVER_NAME'], $_SERVER['SCRIPT_NAME'])), 'response_type' => 'code', 'client_id' => CLIENT_ID, 'approval_prompt' => 'force'));
    $url = $curl->responseHeaders['Location'];
    echo '<a href="' . $url . '">Continue</a>';
}
require '../src/Curl/Curl.php';
use ZzhhCurl\Curl;
define('CLIENT_ID', 'XXXXXXXXXXXX.apps.googleusercontent.com');
define('CLIENT_SECRET', 'XXXXXXXXXXXXXXXXXXXXXXXX');
session_start();
if (isset($_GET['code'])) {
    $code = $_GET['code'];
    // Exchange the authorization code for an access token.
    $curl = new Curl();
    $curl->post('https://accounts.google.com/o/oauth2/token', array('code' => $code, 'client_id' => CLIENT_ID, 'client_secret' => CLIENT_SECRET, 'redirect_uri' => implode('', array(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http', '://', $_SERVER['SERVER_NAME'], $_SERVER['SCRIPT_NAME'])), 'grant_type' => 'authorization_code'));
    if ($curl->error) {
        echo $curl->response->error . ': ' . $curl->response->error_description;
        exit;
    }
    $_SESSION['access_token'] = $curl->response->access_token;
    header('Location: ?');
} elseif (!empty($_SESSION['access_token'])) {
    // Use the access token to send an email.
    $curl = new Curl();
    $curl->setHeader('Content-Type', 'message/rfc822');
    $curl->setHeader('Authorization', 'OAuth ' . $_SESSION['access_token']);
    $boundary = md5(time());
    $raw = 'MIME-Version: 1.0' . "\r\n" . 'Subject: hi' . "\r\n" . 'To: John Doe <*****@*****.**>' . "\r\n" . 'Content-Type: multipart/alternative; boundary=' . $boundary . "\r\n" . "\r\n" . '--' . $boundary . "\r\n" . 'Content-Type: text/plain; charset=UTF-8' . "\r\n" . "\r\n" . 'hello, world' . "\r\n" . "\r\n" . '--' . $boundary . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n" . "\r\n" . '<em>hello, world</em>' . "\r\n" . '';
    $curl->post('https://www.googleapis.com/upload/gmail/v1/users/me/messages/send', $raw);
    echo 'Email ' . $curl->response->id . ' was sent.';
} else {
    $curl = new Curl();
    $curl->get('https://accounts.google.com/o/oauth2/auth', array('scope' => 'https://www.googleapis.com/auth/gmail.compose', 'redirect_uri' => implode('', array(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http', '://', $_SERVER['SERVER_NAME'], $_SERVER['SCRIPT_NAME'])), 'response_type' => 'code', 'client_id' => CLIENT_ID, 'approval_prompt' => 'force'));
    $url = $curl->responseHeaders['Location'];
    echo '<a href="' . $url . '">Continue</a>';
}