3 function url_fetch($url)
7 curl_setopt($ch, CURLOPT_URL, $url);
8 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
9 $user_agent = "Mozilla/5.0 (compatible; dabr; " . BASE_URL . ")";
10 curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
11 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
12 $fetch_start = microtime(1);
13 $response = curl_exec($ch);
16 $services_time += microtime(1) - $fetch_start;
20 function oembed_embed_thumbnails(&$feed)
22 foreach($feed as &$status)
23 { // Loop through the feed
24 if(stripos($status->text, 'NSFW') === FALSE)
25 { // Ignore image fetching for tweets containing NSFW
26 if ($status->entities)
27 { // If there are entities
28 $entities = $status->entities;
31 foreach($entities->urls as $urls)
32 { // Loop through the URL entities
33 if($urls->expanded_url != "") { // Use the expanded URL, if it exists, to pass to Oembed provider
34 $url = $urls->expanded_url;
39 $matched_urls[urlencode($url)][] = $status->id;
42 // No URLs, do nothing
43 $matched_urls[] = null;
49 // Reduce the array of empty items
50 foreach ($matched_urls as $key => $value)
54 unset($matched_urls[$key]);
58 // Make a single API call to Embedkit.
59 if(defined('EMBEDKIT_KEY') && EMBEDKIT_KEY != "")
61 // Only the URLs which we're going to pass to Embedkit
62 $justUrls = array_keys($matched_urls);
64 $count = count($justUrls);
65 if ($count == 0) return;
67 // // Things can slow down with lots of links.
68 // $justUrls = array_chunk ($justUrls, 10);
69 // $justUrls = $justUrls[0];
71 $url = 'https://embedkit.com/api/v1/embed?key='.EMBEDKIT_KEY.'&urls=' . implode(',', $justUrls) . '&format=json';
73 $embedly_json = url_fetch($url);
74 $oembeds = json_decode($embedly_json);
76 if($oembeds->type != 'error')
78 // Single statuses don't come back in an array
79 if (!is_array($oembeds))
81 $temp = array(0 => $oembeds);
85 foreach ($justUrls as $index => $url)
90 if ($oembeds[$index]->thumbnail_url) {
91 // Direct links to files
92 $thumb = $oembeds[$index]->thumbnail_url;
95 if ($oembeds[$index]->title) {
96 // Direct links to files
97 $title = $oembeds[$index]->title;
101 { // Embed the thumbnail
102 $html = theme('external_link', urldecode($url), "<img src=\"" . image_proxy($thumb, "") . "\"" .
103 " title=\"{$title}\" alt=\"{$title}\" class=\"embedded\" />");
104 foreach ($matched_urls[$url] as $statusId)
106 $feed[$statusId]->text = $feed[$statusId]->text . '<br />' . '<span class="embed">' . $html . '</span>';
108 } elseif ($title) { // Embed a link
109 $html = theme('external_link', urldecode($url), $title);
110 foreach ($matched_urls[$url] as $statusId)
112 $feed[$statusId]->text = $feed[$statusId]->text . '<br />' . '<span class="embed">' . $html . '</span>';