Jump to content

PHP script setup for multiple readouts, only outputs one


David Nelson

Recommended Posts

Hey guys,

 

I'm using a script to scrape Google images (http://blogoscoped.com/archive/2007-03-19-n36.html), you can take his source code there to look at it, but the problem is in his code it only displays the first result from the Google images results page -- however, his demo (http://blogoscoped.com/temp/google-image-scraper-2007.html) he setup does the entire first page of Google image results, but he doesn't explain how to do it.

 

I've messed with it alot and can't figure out what is in that code that you could modify to make it display multiple results. I've attached the code below;

 

(btw, this script essentially scrapes from http://images.google.com/images?hl=en&q=horses&btnG=Search+Images&gbv=2 or whatever you input as the variable, [his demo is 'horses'], in case you didn't get what I'm talking about)

 

Thanks guys!

 

<?

$results = getGoogleImages('horses');
foreach ($results as $result) {
    echo '<p><a href="' . htmlentities($result['url']) . '">' .
            '<img src="' . htmlentities($result['thumbnail']) . '" alt="" ' .
            'oncontextmenu="this.src=\'' . htmlentities($result['image']) . '\';return false;" ' .
            'style="border: 1px solid black" /></a><br />' .
            '<em>' . htmlentities($result['description']) . '</em>' .
            '</p>';
}

?>

</body>
</html><?

function getGoogleImages($q, $doSafeSearch = false)
{
    $results = array();

    $safe = ($doSafeSearch) ? 'on' : 'off';
    $url = 'http://images.google.com/images?safe=' . $safe .
            '&q=' . urlencode($q);
    $result = file_get_contents($url);

    $from = 'dyn.Img("';
    $startPos = strPos($result, $from);
    $endPos = strPos($result, ');dyn.updateStatus');
    $functions = substr( $result, $startPos + strlen($from), $endPos );
    $functions = explode('");dyn.Img("', $functions);

    foreach ($functions as $f) {
        $i = count($results);
        list($results[$i]['url'], $v1, $hash,
                $results[$i]['image'],
                $results[$i]['width'], $results[$i]['height'],
                $results[$i]['description'],
                $v2, $v3, $more, $extension, $domain) = explode('","', $f);
        list($results[$i]['url'], $params) = explode('&h', $results[$i]['url']);

        $prefix = 'http://tbn0.google.com/images?q=tbn:';
        $results[$i]['thumbnail'] = $prefix . $hash . ':' . $results[$i]['image'];
        $results[$i]['description'] = strip_tags($results[$i]['description']);
    }

    return $results;
}

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.