+ /**
+ * Convert json to XML.
+ */
+ private static function jsonToXml($json, $root_node_name) {
+ // Apparently the ObjectNormalizer used afterwards is not able to handle
+ // the stdClass object created by json_decode() with the default setting
+ // $assoc = false; so use $assoc = true.
+ $data = json_decode($json, $assoc = TRUE);
+ if (!$data) {
+ return NULL;
+ }
+
+ $encoder = new XmlEncoder();
+ $normalizer = new ObjectNormalizer();
+ $serializer = new Serializer(array($normalizer), array($encoder));
+
+ $serializer_options = array(
+ 'xml_encoding' => "UTF-8",
+ 'xml_format_output' => TRUE,
+ 'xml_root_node_name' => $root_node_name,
+ );
+
+ $xml_data = $serializer->serialize($data, 'xml', $serializer_options);
+ if (!$xml_data) {
+ trigger_error("Cannot serialize data", E_USER_ERROR);
+ return NULL;
+ }
+
+ return $xml_data;
+ }
+
+ /**
+ * Convert the Instagram content to XML.
+ */
+ private function getXmlInstagramCom($html) {
+ // Extract the json data from the html code.
+ $json_match_expr = '/window._sharedData = (.*);/';
+ $ret = preg_match($json_match_expr, $html, $matches);
+ if ($ret !== 1) {
+ trigger_error("Cannot match expression: $json_match_expr\n", E_USER_ERROR);
+ return NULL;
+ }
+
+ return Tweeper::jsonToXml($matches[1], 'instagram');
+ }
+
+ /**
+ * Make the Facebook HTML processable.
+ */
+ private function preprocessHtmlFacebookCom($html) {
+ $html = str_replace('<!--', '', $html);
+ $html = str_replace('-->', '', $html);
+ return $html;
+ }
+
+ /**
+ * Convert the HTML retrieved from the site to XML.
+ */
+ private function htmlToXml($html, $host) {
+ $xmlDoc = new DOMDocument();
+
+ // Handle warnings and errors when loading invalid HTML.
+ $xml_errors_value = libxml_use_internal_errors(TRUE);
+
+ // If there is a host-specific method to get the XML data, use it!
+ $get_xml_host_method = 'getXml' . Tweeper::toUpperCamelCase($host, '.');
+ if (method_exists($this, $get_xml_host_method)) {
+ $xml_data = call_user_func_array(array($this, $get_xml_host_method), array($html));
+ $xmlDoc->loadXML($xml_data);
+ }
+ else {
+ $xmlDoc->loadHTML($html);
+ }
+
+ foreach (libxml_get_errors() as $xml_error) {
+ Tweeper::logXmlError($xml_error);
+ }
+ libxml_clear_errors();
+ libxml_use_internal_errors($xml_errors_value);
+
+ return $xmlDoc;
+ }
+
+ /**
+ * Load a stylesheet if the web site is supported.
+ */
+ private function loadStylesheet($host) {