Getting latest Sub Reddit posts with PHP

Fetching a Sub Reddits latest posts is actually easy, mostly because the Reddit API is free and you don’t need a key.

You can do self archiving with PHP and MySQL very quickly.

Using the free Reddit api we can get the latest (newest) 50 posts/submits to  a sub using the following JSON endpoint Note that you can return upto 100 results, but if you call the script every 1 or 2 minutes you will just be wasting resources.

Here is PHP code that also fetches data from the returned API JSON:

$data = json_decode(file_get_contents(""), true);
foreach ($data['data']['children'] as $val) {
    $title = $val['data']['title'];
    $domain = $val['data']['domain'];
    $url = $val['data']['url'];
    $reddit_link = $val['data']['permalink'];
    $post_id = $val['data']['id'];
    $date = $val['data']['created_utc'];
    $user = $val['data']['author'];
    $flair = $val['data']['author_flair_text'];
    $up = $val['data']['ups'];
    $comments = $val['data']['num_comments'];
    $thumbnail = $val['data']['secure_media']['oembed']['thumbnail_url'];

I have included examples to access some of the data in each of the returned “children” but not all..

          "approved_at_utc": null,
          "subreddit": "pics",
          "selftext": "",
          "author_fullname": "t2_gatxq",
          "saved": false,
          "mod_reason_title": null,
          "gilded": 0,
          "clicked": false,
          "title": "Anybody checked out the beautiful new year's full moon yesterday? Heres a pic i took",
          "link_flair_richtext": [],
          "subreddit_name_prefixed": "r/pics",
          "hidden": false,
          "pwls": 6,
          "link_flair_css_class": null,
          "downs": 0,
          "thumbnail_height": 140,
          "hide_score": true,
          "name": "t3_abhzu9",
          "quarantine": false,
          "link_flair_text_color": "dark",
          "author_flair_background_color": null,
          "subreddit_type": "public",
          "ups": 5,
          "domain": "",
          "media_embed": {},
          "thumbnail_width": 140,
          "author_flair_template_id": null,
          "is_original_content": false,
          "user_reports": [],
          "secure_media": null,
          "is_reddit_media_domain": true,
          "is_meta": false,
          "category": null,
          "secure_media_embed": {},
          "link_flair_text": null,
          "can_mod_post": false,
          "score": 5,
          "approved_by": null,
          "thumbnail": "",
          "edited": false,
          "author_flair_css_class": null,
          "author_flair_richtext": [],
          "gildings": {
            "gid_1": 0,
            "gid_2": 0,
            "gid_3": 0
          "post_hint": "image",
          "content_categories": [
          "is_self": false,
          "mod_note": null,
          "created": 1546376127,
          "link_flair_type": "text",
          "wls": 6,
          "banned_by": null,
          "author_flair_type": "text",
          "contest_mode": false,
          "selftext_html": null,
          "likes": null,
          "suggested_sort": null,
          "banned_at_utc": null,
          "view_count": null,
          "archived": false,
          "no_follow": false,
          "is_crosspostable": true,
          "pinned": false,
          "over_18": false,
          "preview": {
            "images": [
                "source": {
                  "url": ";s=1f2b2b0759605ee0bf613388caf6ec9794d22388",
                  "width": 3120,
                  "height": 4160
                "resolutions": [
                    "url": ";crop=smart&amp;auto=webp&amp;s=2acad265bf6fc57cb36f12de49910bb9148fdae1",
                    "width": 108,
                    "height": 144
                    "url": ";crop=smart&amp;auto=webp&amp;s=fccaefe166c013601c48f32bfedca45347ec3922",
                    "width": 216,
                    "height": 288
                    "url": ";crop=smart&amp;auto=webp&amp;s=deffeb956d186002daebce223220874a72967431",
                    "width": 320,
                    "height": 426
                    "url": ";crop=smart&amp;auto=webp&amp;s=8c562de131301c0e759e4c6248f0967024f11360",
                    "width": 640,
                    "height": 853
                    "url": ";crop=smart&amp;auto=webp&amp;s=c5b519c9392c25915ccce5d3d19844de01901915",
                    "width": 960,
                    "height": 1280
                    "url": ";crop=smart&amp;auto=webp&amp;s=f67def5fd07e9a97e9da839680f66d393b99225e",
                    "width": 1080,
                    "height": 1440
                "variants": {},
                "id": "AFOMmV7YjpTdqfBPasT-qqrseYBgrbJvqDySHVNN2cg"
            "enabled": true
          "media_only": false,
          "link_flair_template_id": null,
          "can_gild": true,
          "spoiler": false,
          "locked": false,
          "author_flair_text": null,
          "visited": false,
          "num_reports": null,
          "distinguished": null,
          "subreddit_id": "t5_2qh0u",
          "mod_reason_by": null,
          "removal_reason": null,
          "link_flair_background_color": "",
          "id": "abhzu9",
          "is_robot_indexable": true,
          "report_reasons": null,
          "author": "rrasic",
          "num_crossposts": 0,
          "num_comments": 0,
          "send_replies": true,
          "whitelist_status": "all_ads",
          "mod_reports": [],
          "author_patreon_flair": false,
          "author_flair_text_color": null,
          "permalink": "/r/pics/comments/abhzu9/anybody_checked_out_the_beautiful_new_years_full/",
          "parent_whitelist_status": "all_ads",
          "stickied": false,
          "url": "",
          "subreddit_subscribers": 20266416,
          "created_utc": 1546347327,
          "media": null,
          "is_video": false

There is a full post data.