Development

Scraping Steam screenshots with PHP

Here is how to scrape a Steam games screenshots with PHP. It’s actually very simple.

All games on Steam have a community hub which has news, videos, forums, guides, screenshots and more.

You can browse Steam screenshots organised by: Most popular (day/week/3months/6months/year/all time) or most recent. This code uses most recent, you can also append pages to the url with p= like so:

https://steamcommunity.com/app/730/screenshots/?p=3&browsefilter=trendsixmonths

Note the number after app/ that is the game id 730 is Counter Strike Global Offensive, that’s what this code will scrape.

Here is the PHP code with comments to help you understand what each line of the code does:

$html = file_get_contents('https://steamcommunity.com/app/730/screenshots/?p=1&browsefilter=mostrecent');//CSGO most recent screenshots
$dom = new domDocument;//New DOM
@$dom->loadHTML($html);//put html into the DOM
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');//every getAttribute('src');//Gets the src from the img tags, src is the image url
    if (strpos($url, '/ugc/') !== false) {//Only work with image urls containing /ugc/     these are ALWAYS the screenshots
        $image_full = substr($url, 0, strpos($url, "?interpolation"));//removes everything after and including ?interpolation      this cleans the url
        $array = explode("/", $image_full);//Splits the url up in an array from the /
        if (isset($array[5])) {
            $file_id = $array[5];//https://steamuserimages-a.akamaihd.net/ugc/951844356900293445/DA3030AB032C01EAE1578F553EFCF0ABC89DD6C8/ gets the info in between the last / /
            file_put_contents("steam/screenshots/csgo/$file_id.jpg", file_get_contents($image_full));//save the image to the directory
            //echo "
";//Show images saved $x++;//success counter } else { $y++;//issue counter } } } echo "Saved $x images, had issues with $y";

For most part the only things you need to change are the appid (game), page, screenshot organisation (set at most recent) and the directory to save the screenshots into (currently it is set to: steam/screenshots/csgo/).

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago