views:

395

answers:

3

Hello,

When sending a message on Facebook, if you include a URL it generally grabs a picture from the webpage and adds it at the bottom as a thumbnail. You then have the ability to select through a number of pictures featured on the site.

I can see how this could be built, but to save me the hassle I wonder if somebody has already done it already in a publicly available format?

Thanks!

A: 

Really shouldn't be hard to implement. Coding is fun, take this and roll with it:

$ch = curl_init();

curl_setopt_array($ch, array(CURLOPT_URL => $_POST['url'], CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER, true));

$results = curl_exec($ch);

$doc = new DOMDocument();

$doc->loadHTML($results);

$images = $doc->getElementsByTagName('img');

This should return a DOMNodeList I believe -- from there you iterate through and pull out the src attribute for each image, stick it into json_encode(), and then write a nice webservice to submit a url and return the nice little collection of images.

I realize its not what you're asking for, but its a start.

Skone
thanks mate,it's more so the snazzy UI that FB has that I think will take the time to get built.. no big deal if i have to build it from scratch i guess... just always try and avoid reinventing the wheel.
Pete
I'm trying to run the example,I have tried to implement a 'basic' script:<?$ch = curl_init();$url = "http://www.altavista.com";curl_setopt_array($ch, array(CURLOPT_URL => $url, CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER, true));$results = curl_exec($ch);$doc = new DOMDocument();$doc->loadHTML($results);$images = $doc->getElementsByTagName('img');var_dump($images);?>however i get this back:Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Empty string supplied as input in /Applications/MAMP/htdocs/raw.php on line 11object(DOMNodeList)#2 (0) { }
Pete
i wish i knew how to paste code here, sorry i'm a newbie.. spent 20 minutes trying to work it out :(
Pete
Give me till this evening when I'm off -- I'll take a look and help you out :)
Skone
A: 

See:

gabrielk
thanks gabrielk, i'm not after a screenshot of a website.. rather the individual images from that website
Pete
+2  A: 

Alright, the code sample I prepared for you was too long to add as a comment of the first. So here's the correct code, verified to work on my local PHP environment (5.3.1):

<?php
/**
 * Sample CURL Image Extractor
 * 
 * Prepared for stackoverflow.com 
 *
 * @author Sam Skjonsberg <[email protected]>
 **/

if(!function_exists('json_encode'))
{
    die('You need at least PHP 5.2 to use this script.');
}

//
// JSON & No-Cache Headers
// Uncoment when implemented as an actual JSON service
//
//header('Cache-Control: no-cache, must-revalidate');
// Date in the past to ensure it isn't cached
//header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
//header('Content-type: application/json');
//

//$url      =  parse_url($_REQUEST['url']);
// Harcoded url for demonstration
// Shameless self-plug :)
$url        = 'http://www.codeviking.net';

if(!empty($url))
{       

    if(!preg_match('%^https?://%i', $url))
    {
        echo get_json_error('Invalid URL');
    }

    $ch     = curl_init();

    curl_setopt_array(  
                        $ch,    
                        array
                        (
                            CURLOPT_URL             => $url,
                            CURLOPT_RETURNTRANSFER  => true,
                            CURLOPT_FOLLOWLOCATION  => true
                        )
                     );

    $html   = curl_exec($ch);

    if(!empty($html)) 
    {
        $doc                = new DOMDocument($html);

        $doc->loadHTML($html);

        $images             = $doc->getElementsByTagName('img');

        $image_srcs         = array();

        foreach($images as $img) {
            foreach($img->attributes as $attribute_name => $attribute_node)
            {
                if($attribute_name = 'src')
                {
                    $src            = $attribute_node->nodeValue;

                    // Parse image into absolute URL
                    if(!preg_match('%^https?://%i', $src))
                    {
                        $src    = $url . '/' . preg_replace('%^\.?/%', '', $src);       
                    }

                    $image_srcs[]   = $src;

                }
            }
        }

        echo json_encode($image_srcs);  

        // And there you have it, a collection of image
        // paths to parse through on the client and add <img src="image_src" /> for each.
        //
        // So, for instance as your ajax success callback
        //
        //
        // var images = parseJSON(image_json);
        // for(var i = 0; i < images.length; i++)
        // {
        //  image_src = images[i];
        //  /* Requires jQuery */
        //  $('body').append($('<img />').attr('src', image_src));
        // }
    } 
    else 
    {
        echo get_json_error('Invalid URL');
    }
} 
else 
{
    echo get_json_error('Invalid URL');
}

function get_json_error($msg)
{   
    $error  = array('error' => $msg);
    return json_encode($error);
}

That really should work. Also I'd appreciate a vote up on the answer as I'm trying to break the 100 point mark! Thanks and good luck!

Skone
Did this not work for ya?
Skone