3

When posting a link (external web page) to my Drupal blog, is there a way to have Drupal automatically pull the picture and summary information from that link and include it in the post on my Drupal blog the way Facebook does it when a person posts a link from an external web page on their Facebook wall or group?

If it is possible, can somebody please point me in the right direction on how to do it.

0

AFAIK there is no module available for this specific use case. However FBSMP module has its own implementation of crawling links/image/video and other media types from the given URL.

You may need to take that as a base and develop your own version.

Below is the sample code on how its crwaling the links

$result = drupal_http_request($url, array('timeout' => $options['timeout']));

  if ($result->code != 200) {
    fbsmp_log_error(t("Could not connect to @url, HTTP error @error", array('@error' => $result->code, '@url' => $url)));
    return $data;
  }

  $page_url = $url;
  if (in_array($result->redirect_code, array(301, 302, 307))) {
    $page_url = $result->redirect_url;
    $data['title'] = $page_url;
  }

  if (stripos($result->headers['Content-Type'], 'image') !== FALSE) {
    $images = array();
    if (($imagesize = _fbsmp_link_validate_filesize($page_url, $options['max_imagesize'], $options['min_imagesize'], ($options['timeout'] - timer_read(__FUNCTION__) / 1000))) != -1) {
      $images[$page_url] = $imagesize;
    }

    $data['images'] = $images;
  }
  else {
    _fbsmp_link_detect_charset($result->data);

    $document = new DOMDocument();
    if (@$document->loadHTML($result->data) === FALSE) {
      fbsmp_log_error(t("Could not parse the content on @url", array('@url' => $url)));
      return $data;
    }

    $xpath         = new DOMXPath($document);

    //Get title of the page.
    $titles        = @$xpath->evaluate("//title");
    $data['title'] = trim($titles->length > 0 ? $titles->item(0)->nodeValue : $data['title']);
    //Get description of the page.
    $descriptions  = @$xpath->evaluate("//meta[@name='description']");
    $data['description'] = trim($descriptions->length > 0 ? $descriptions->item(0)->getAttribute('content') : $data['description']);

    if (!$emfield['emvideo_valid'] && !$emfield['emaudio_valid']) {
      $hrefs         = @$xpath->evaluate("/html/body//img");
      $images = array();

      if (!fbsmp_include_library('url_to_absolute.inc', 'url_to_absolute')) {
        fbsmp_log_error(t("Could not find url_to_absolute.inc file in the libraries folder"));
        return $data;
      }

      for ($i = 0; $i < $hrefs->length; $i++) {
        $image_url = $hrefs->item($i)->getAttribute('src');
        if (!isset($image_url) || empty($image_url)) {
          continue;
        }
        $abs_url = url_to_absolute($page_url, $image_url);
        if ($abs_url && ($imagesize = _fbsmp_link_validate_filesize($abs_url, $options['max_imagesize'], $options['min_imagesize'], ($options['timeout'] - timer_read(__FUNCTION__) / 1000))) != -1) {
          $images[$abs_url] = $imagesize;
        }
        if (($options['timeout'] - timer_read(__FUNCTION__) / 1000) <= 0) {
          fbsmp_log_error(t("Request timed out for @url", array('@url' => $url)));
          break;
        }
      }
      asort($images);
      $images = array_reverse($images, TRUE);
      $data['images'] = $images;
    }
  }

  //Cache the results.
  if ($options['cached']) {
    cache_set($arghash, $data, 'cache', REQUEST_TIME + $options['cache_duration']);
  }

You may also check following references,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.