/**
  *
  * Gets the oEmbed data from the Driver Source, returned as an array
  *
  * @param array $params - parameters for the oEmbed API request
  * @param bool $errorFlag - ref parameter to flag if the operation was successful (new in 1.3)
  *
  * @return array
  * 			url => the url uses to get the data
  * 			xml => the raw xml data
  * 			json => the raw jason data, if any
  * 			id => the id the resource
  * 			dirver => the driver's name used for this resource
  * 			title => the title of the ressource
  * 			thumb => the thumbnail of the resource, if any
  * 			error => the error message, if any
  */
 public final function getDataFromSource($params, &$errorFlag)
 {
     // assure we have no error
     $errorFlag = false;
     // get the complete url
     $url = $this->getOEmbedApiUrl($params);
     // create the Gateway object
     $gateway = new Gateway();
     // set our url
     $gateway->init($url);
     // get the raw response, ignore errors
     $response = @$gateway->exec();
     // declare the result array
     $data = array();
     // add url to array
     $data['url'] = $url;
     // add driver to array
     $data['driver'] = $this->getName();
     // if we have a valid response
     if (!$response || strlen($response) < 1) {
         $errorFlag = true;
         $data['error'] = __('Failed to load oEmbed data');
     } else {
         // get the good parser for the service format
         // fixes Issue #15
         $parser = ServiceParser::getServiceParser($this->getAPIFormat());
         $parsedAray = @$parser->createArray($response, $this, $url, $errorFlag);
         if (!$errorFlag && $parsedAray !== FALSE) {
             // merge the parsed data
             $data = array_merge($data, $parsedAray);
         } else {
             $errorFlag = true;
             $data['error'] = __('Failed to parse oEmbed data: %s', array($parsedAray['error']));
         }
     }
     return $data;
 }
 /**
  * Appends data into the XML tree of a Data Source
  * @param $wrapper
  * @param $data
  */
 public function appendFormattedElement(&$wrapper, $data)
 {
     if (!is_array($data) || empty($data) || empty($data['url'])) {
         return;
     }
     // If cache has expired refresh the data array from parsing the API XML
     /*if ((time() - $data['last_updated']) > ($this->_fields['refresh'] * 60)) {
     			$data = VimeoHelper::updateClipInfo($data['clip_id'], $this->_fields['id'], $wrapper->getAttribute('id'), $this->Database);
     		}*/
     // store a pointer to the driver
     // @todo: use the `driver` column
     $driver = ServiceDispatcher::getServiceDriver($data['url']);
     if ($driver == null) {
         throw new Exception('Unable to find driver for url: `' . $data['url'] . '`');
     }
     $apiFormat = $driver->getAPIFormat();
     $parser = ServiceParser::getServiceParser($apiFormat);
     if ($parser == null) {
         throw new Exception('Unable to find parser for format: `' . $apiFormat . '`');
     }
     // root for all values
     $field = new XMLElement($this->get('element_name'));
     $field->setAttributeArray(array('id' => $data['res_id']));
     $title = new XMLElement('title', General::sanitize($data['title']));
     $title->setAttribute('handle', Lang::createHandle($data['title']));
     $field->appendChild($title);
     $field->appendChild(new XMLElement('url', General::sanitize($data['url'])));
     $field->appendChild(new XMLElement('thumbnail', General::sanitize($data['thumbnail_url'])));
     $field->appendChild(new XMLElement('driver', General::sanitize($data['driver'])));
     $protocols = new XMLElement('protocols');
     if ($driver->supportsSSL()) {
         $protocols->appendChild(new XMLElement('item', 'https'));
     }
     $protocols->appendChild(new XMLElement('item', 'http'));
     $field->appendChild($protocols);
     // oembed data
     $xml = new DomDocument('1.0', 'utf-8');
     $errorFlag = false;
     // use our parser in order to get the xml string
     $xml_data = $parser->createXML($data['oembed_xml'], $driver, $data['url'], $errorFlag);
     // if we can successfully load the XML data into the
     // DOM object while ignoring errors (@)
     if (!$errorFlag && @$xml->loadXML($xml_data)) {
         $xml->preserveWhiteSpace = true;
         $xml->formatOutput = true;
         $xml->normalizeDocument();
         $root_name = $driver->getRootTagName();
         // get the root node
         $xml_root = $xml->getElementsByTagName($root_name)->item(0);
         // if we've found a root node
         if (!empty($xml_root)) {
             // save it as a string
             $xml = $xml->saveXML($xml_root);
             // replace the 'root' element with 'oembed'
             if ($root_name != 'oembed') {
                 $xml = preg_replace('/^<' . $root_name . '>/', '<oembed>', $xml);
                 $xml = preg_replace('/<\\/' . $root_name . '>/', '</oembed>', $xml);
             }
             // set it as the 'value' of the field
             // BEWARE: it will be just a string, since the
             // value we set is xml. It's just a hack to pass
             // the value from the DOMDocument object to the XMLElement
             $field->setValue($xml, false);
         } else {
             $errorFlag = true;
         }
     } else {
         $errorFlag = true;
     }
     if ($errorFlag) {
         // loading the xml string into the DOMDocument did not work
         // so we will add a errors message into the result
         $error = new XMLElement('error');
         $error->setValue(__('Error while loading the xml into the document'));
         $field->appendChild($error);
     }
     $wrapper->appendChild($field);
 }