getKey('basepath') . '/?') === false) { $this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.'; return; } $data = file_get_contents( $conf->getKey('apiurl', 'yourls'), false, stream_context_create( array( 'http' => array( 'method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\r\n", 'content' => http_build_query( array( 'signature' => $conf->getKey('signature', 'yourls'), 'format' => 'json', 'action' => 'shorturl', 'url' => $link, ) ), ), ) ) ); if ($data === false || !is_string($data)) { $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".'; return; } try { $data = Json::decode($data); } catch (Exception $e) { $this->_error = $e->getMessage(); return; } if ( !is_null($data) && array_key_exists('statusCode', $data) && array_key_exists('shorturl', $data) && $data['statusCode'] == 200 ) { $this->_url = $data['shorturl']; } else { $this->_error = 'Error parsing YOURLS response.'; } } /** * Returns the (untranslated) error message * * @access public * @return string */ public function getError() { return $this->_error; } /** * Returns the shortened URL * * @access public * @return string */ public function getUrl() { return $this->_url; } /** * Returns true if any error has occurred * * @access public * @return bool */ public function isError() { return !empty($this->_error); } }