For media types registered in $_handlers which include an 'decode' setting, decodes data
according to the specified media type.
public static decode ( string $type, mixed $data, array $options = [] ) : mixed | ||
$type | string | Specifies the media type into which `$data` will be encoded. This media type must have an `'encode'` setting specified in `Media::$_handlers`. |
$data | mixed | Arbitrary data you wish to encode. Note that some encoders can only handle arrays or objects. |
$options | array | Handler-specific options. |
return | mixed |
/** * Deliver a message with Mailgun's HTTP REST API via curl. * * _NOTE: Uses the `messages.mime` API endpoint, not the * `messages` API endpoint (because a, if embedded attachments * were used Mailgun would alter the `Content-ID` for them, and * b, cURL needs to have a local file to send as file, but * not all attachments have a path), see `_parameters()`._ * * @see li3_mailer\net\mail\transport\adapter\Mailgun::_parameters() * @see http://documentation.mailgun.net/api-sending.html * @see http://php.net/curl * @param object $message The message to deliver. * @param array $options Options (see `_parameters()`). * @return string The message id on success; `false` on error. */ public function deliver($message, array $options = array()) { list($url, $auth, $parameters) = $this->_parameters($message, $options); $curl = curl_init($url); curl_setopt_array($curl, array(CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => "{$auth['username']}:{$auth['password']}", CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $parameters)); $result = curl_exec($curl); $info = curl_getinfo($curl); if ($info['http_code'] != '200') { $result = false; } curl_close($curl); $result = Media::decode('json', $result); return $result['id']; }
public function testMediaEncoding() { $data = array('hello', 'goodbye', 'foo' => array('bar', 'baz' => 'dib')); $expected = json_encode($data); $result = Media::encode('json', $data); $this->assertEqual($expected, $result); $this->assertEqual($result, Media::to('json', $data)); $this->assertNull(Media::encode('badness', $data)); $result = Media::decode('json', $expected); $this->assertEqual($data, $result); }