/** * Invoke a Slack API method. */ private static function api($method, $params) { return json_decode(remote::post('https://slack.com/api/' . $method, ['data' => array_merge($params, ['token' => c::get('slack.auth')])]), true); }
static function submit_translations() { // Request format (HTTP POST): // client_token = <client_token> // uid = <l10n server user id> // signature = md5(user_api_key($uid, $client_token) . $data . $client_token)) // data = // JSON payload // // {<key_1>: {message: <JSON encoded message> // translations: {<locale_1>: <JSON encoded translation>, // <locale_2>: ...}}, // <key_2>: {...} // } // @todo Batch requests (max request size) // @todo include base_revision in submission / how to handle resubmissions / edit fights? foreach (Database::instance()->select("key", "message", "locale", "base_revision", "translation")->from("outgoing_translations")->get() as $row) { $key = $row->key; if (!isset($request->{$key})) { $request->{$key}->message = json_encode(unserialize($row->message)); } $request->{$key}->translations->{$row->locale} = json_encode(unserialize($row->translation)); } // @todo reduce memory consumption, e.g. free $request $request_data = json_encode($request); $url = self::_server_url() . "?q=translations/submit"; $signature = self::_sign($request_data); list($response_data, $response_status) = remote::post($url, array("data" => $request_data, "client_token" => self::client_token(), "signature" => $signature, "uid" => self::server_uid())); if (!remote::success($response_status)) { throw new Exception("@todo TRANSLATIONS_SUBMISSION_FAILED " . $response_status); } if (empty($response_data)) { return; } $response = json_decode($response_data); // Response format (JSON payload): // [{key:<key_1>, locale:<locale_1>, rev:<rev_1>, status:<rejected|accepted|pending>}, // {key:<key_2>, ...} // ] // @todo Move messages out of outgoing into incoming, using new rev? // @todo show which messages have been rejected / are pending? }
/** * Perform an API request at the Akismet service. * * @param string $url API endpoint url. * @param array $data Parameters to send with the request. * * @return RemoteResponse|Error */ protected function request($url, $data = array()) { $defaults = array('blog' => $this->blog); $response = remote::post($url, array('agent' => $this->agent(), 'timeout' => 60, 'data' => array_merge($defaults, $data))); $error = null; if ($response->code !== 200) { $msg = a::get($response->headers, 'X-akismet-debug-help', 'Invalid API request'); $error = new Error('Akismet: ' . $msg); } else { if ($response->error) { $error = new Error($response->message, $response->error); } } if ($this->fail && !is_null($error)) { throw $error; } return is_null($error) ? $response : $error; }
} $url = 'https://api.mailgun.net/v2/' . $email->options['domain'] . '/messages'; $auth = base64_encode('api:' . $email->options['key']); $headers = array('Accept: application/json', 'Authorization: Basic ' . $auth); $data = array('from' => $email->from, 'to' => $email->to, 'subject' => $email->subject, 'text' => $email->body, 'h:Reply-To' => $email->replyTo); $email->response = remote::post($url, array('data' => $data, 'headers' => $headers)); if ($email->response->code() != 200) { throw new Error('The mail could not be sent!'); } }; /** * Postmark mail driver */ email::$services['postmark'] = function ($email) { if (empty($email->options['key'])) { throw new Error('Invalid Postmark API Key'); } // reset the api key if we are in test mode if (a::get($email->options, 'test')) { $email->options['key'] = 'POSTMARK_API_TEST'; } // the url for postmarks api $url = 'https://api.postmarkapp.com/email'; $headers = array('Accept: application/json', 'Content-Type: application/json', 'X-Postmark-Server-Token: ' . $email->options['key']); $data = array('From' => $email->from, 'To' => $email->to, 'ReplyTo' => $email->replyTo, 'Subject' => $email->subject, 'TextBody' => $email->body); // fetch the response $email->response = remote::post($url, array('data' => json_encode($data), 'headers' => $headers)); if ($email->response->code() != 200) { throw new Error('The mail could not be sent'); } };
public function trigger($url) { $response = remote::get($url); $html = $response->content(); if (preg_match_all('!\\<link(.*?)\\>!i', $html, $links)) { foreach ($links[0] as $link) { if (!str::contains($link, 'rel="webmention"')) { continue; } if (!preg_match('!href="(.*?)"!', $link, $match)) { continue; } $endpoint = $match[1]; $src = $this->page->url(); $target = $url; // invalid endpoint if (!v::url($endpoint)) { continue; } remote::post($endpoint, array('data' => array('source' => $src, 'target' => $target))); return $endpoint; } } }