protected function loadOAuthAccountData() { $uri = new PhutilURI('https://www.googleapis.com/plus/v1/people/me'); $uri->setQueryParam('access_token', $this->getAccessToken()); $future = new HTTPSFuture($uri); list($status, $body) = $future->resolve(); if ($status->isError()) { $this->tryToThrowSpecializedError($status, $body); throw $status; } $data = json_decode($body, true); if (!is_array($data)) { throw new Exception("Expected valid JSON response from Google account data request, " . "got: " . $body); } return $data; }
protected function loadOAuthAccountData() { $uri = new PhutilURI('https://www.googleapis.com/plus/v1/people/me'); $uri->setQueryParam('access_token', $this->getAccessToken()); $future = new HTTPSFuture($uri); list($status, $body) = $future->resolve(); if ($status->isError()) { $this->tryToThrowSpecializedError($status, $body); throw $status; } try { return phutil_json_decode($body); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException(pht('Expected valid JSON response from Google account data request.'), $ex); } }
#!/usr/bin/env php <?php $root = dirname(dirname(dirname(__FILE__))); require_once $root . '/scripts/__init_script__.php'; $args = new PhutilArgumentParser($argv); $args->parseStandardArguments(); $args->parse(array(array('name' => 'attach', 'param' => 'file', 'help' => pht('Attach a file to the request.')), array('name' => 'url', 'wildcard' => true))); $uri = $args->getArg('url'); if (count($uri) !== 1) { throw new PhutilArgumentUsageException("Specify exactly one URL to retrieve."); } $uri = head($uri); $method = 'GET'; $data = ''; $timeout = 30; $future = new HTTPSFuture($uri, $data); $future->setMethod($method); $future->setTimeout($timeout); $attach_file = $args->getArg('attach'); if ($attach_file !== null) { $future->attachFileData('file', Filesystem::readFile($attach_file), basename($attach_file), Filesystem::getMimeType($attach_file)); } print_r($future->resolve());
public function send() { $user = PhabricatorEnv::getEnvConfig('sendgrid.api-user'); $key = PhabricatorEnv::getEnvConfig('sendgrid.api-key'); if (!$user || !$key) { throw new Exception("Configure 'sendgrid.api-user' and 'sendgrid.api-key' to use " . "SendGrid for mail delivery."); } $params = array(); $ii = 0; foreach (idx($this->params, 'tos', array()) as $to) { $params['to[' . $ii++ . ']'] = $to; } $params['subject'] = idx($this->params, 'subject'); if (idx($this->params, 'is-html')) { $params['html'] = idx($this->params, 'body'); } else { $params['text'] = idx($this->params, 'body'); } $params['from'] = idx($this->params, 'from'); if (idx($this->params, 'from-name')) { $params['fromname'] = $this->params['from-name']; } if (idx($this->params, 'reply-to')) { $replyto = $this->params['reply-to']; // Pick off the email part, no support for the name part in this API. $params['replyto'] = $replyto[0]['email']; } $headers = idx($this->params, 'headers', array()); // See SendGrid Support Ticket #29390; there's no explicit REST API support // for CC right now but it works if you add a generic "Cc" header. // // SendGrid said this is supported: // "You can use CC as you are trying to do there [by adding a generic // header]. It is supported despite our limited documentation to this // effect, I am glad you were able to figure it out regardless. ..." if (idx($this->params, 'ccs')) { $headers[] = array('Cc', implode(', ', $this->params['ccs'])); } if ($headers) { // Convert to dictionary. $headers = ipull($headers, 1, 0); $headers = json_encode($headers); $params['headers'] = $headers; } $params['api_user'] = $user; $params['api_key'] = $key; $future = new HTTPSFuture('https://sendgrid.com/api/mail.send.json', $params); list($code, $body) = $future->resolve(); if ($code !== 200) { throw new Exception("REST API call failed with HTTP code {$code}."); } $response = json_decode($body, true); if (!is_array($response)) { throw new Exception("Failed to JSON decode response: {$body}"); } if ($response['message'] !== 'success') { $errors = implode(";", $response['errors']); throw new Exception("Request failed with errors: {$errors}."); } return true; }