Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8981973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:28:06+00:00 2026-06-15T20:28:06+00:00

My php project is using the reddit JSON api to grab the title of

  • 0

My php project is using the reddit JSON api to grab the title of the current page’s submission.

Right now I am doing running some code every time the page is loaded and I’m running in to some problems, even though there is no real API limit.

I would like to store the title of the submission locally somehow. Can you recommend the best way to do this? The site is running on appfog. What would you recommend?

This is my current code:

<?php

/* settings */

$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$reddit_url = 'http://www.reddit.com/api/info.{format}?url='.$url;

$format = 'json'; //use XML if you'd like...JSON FTW!
$title = '';

/* action */
$content = get_url(str_replace('{format}',$format,$reddit_url)); //again, can be xml or json
if($content) {
    if($format == 'json') {
        $json = json_decode($content,true);
        foreach($json['data']['children'] as $child) { // we want all children for this example
            $title= $child['data']['title'];
        }
    }
}

/* output */


/* utility function:  go get it! */
function get_url($url) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
?>

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:28:08+00:00Added an answer on June 15, 2026 at 8:28 pm

    Introduction

    Here is a modified version of your code

    $url = "http://stackoverflow.com/";
    $loader = new Loader();
    $loader->parse($url);
    printf("<h4>New List : %d</h4>", count($loader));
    printf("<ul>");
    foreach ( $loader as $content ) {
        printf("<li>%s</li>", $content['title']);
    }
    printf("</ul>");
    

    Output

    New List : 7

    • New podcast from Joel Spolsky and Jeff Atwood.
    • Good site for example code/ Pyhton
    • stackoverflow.com has clearly the best Web code ever conceived in the history of the Internet and reddit should better start copying it.
    • A reddit-like, OpenID using website for programmers
    • Great developer site. Get your questions answered and by someone who knows.
    • Stack Overflow launched into public
    • Stack Overflow, a programming Q & A site. & Reddit could learn a lot from their interface!

    Simple Demo

    The Problem

    I see some things you want to achieve here namely

    • I would like to store the title of the submission locally somehow
    • Right now I am doing running some code every time the page is loaded

    From what i understand you need is a simple cache copy of your data so that you don’t have to load the url all the time.

    Simple Solution

    A simple cache system you can use is memcache ..

    Example A

    $url = "http://stackoverflow.com/";
    
    // Start cache
    $m = new Memcache();
    $m->addserver("localhost");
    $cache = $m->get(sha1($url));
    
    if ($cache) {
        // Use cache copy
        $loader = $cache;
        printf("<h2>Cache List: %d</h2>", count($loader));
    } else {
    
        // Start a new Loader
        $loader = new Loader();
        $loader->parse($url);
        printf("<h2>New List : %d</h2>", count($loader));
        $m->set(sha1($url), $loader);
    }
    
    // Oupput all listing
    printf("<ul>");
    foreach ( $loader as $content ) {
        printf("<li>%s</li>", $content['title']);
    }
    printf("</ul>");
    

    Example B

    You can use Last Modification Date as the cache key as so that you would only save new copy only if the document is modified

    $headers = get_headers(sprintf("http://www.reddit.com/api/info.json?url=%s",$url), true);
    $time = strtotime($headers['Date']); // get last modification date 
    $cache = $m->get($time);
    
    if ($cache) {
        $loader = $cache;
    }
    

    Since your class implements JsonSerializable you can json encode your result and also store in a Database like MongoDB or MySQL

     $data = json_encode($loader);
     // Save to DB 
    

    Class Used

    class Loader implements IteratorAggregate, Countable, JsonSerializable {
        private $request = "http://www.reddit.com/api/info.json?url=%s";
        private $data = array();
        private $total;
    
        function parse($url) {
            $content = json_decode($this->getContent(sprintf($this->request, $url)), true);
            $this->data = array_map(function ($v) {
                return $v['data'];
            }, $content['data']['children']);
            $this->total = count($this->data);
        }
    
        public function getIterator() {
            return new ArrayIterator($this->data);
        }
    
        public function count() {
            return $this->total;
        }
    
        public function getType() {
            return $this->type;
        }
    
        public function jsonSerialize() {
            return $this->data;
        }
    
    
        function getContent($url) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
            $content = curl_exec($ch);
            curl_close($ch);
            return $content;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a php programmer now doing a Java web project using Spring framework. I'm
Am using below third party API in my project development http://undesigned.org.za/2007/10/22/amazon-s3-php-class I have done
We're using JSON in some parts of our PHP project. The edit part has
I am using eclipse Indigo to work on java.Now i get one PHP project
we have a php project using zend framework and in a page we have
Im trying to learn php by doing a little project using apache server. I
I have developing one PHP project using eclipse locally. Now i need to share
I'm doing i18n for a php project using gettext. I'd like to use the
im doing a PHP project using codeigniter 2.1.2 , using mySql database on WAMP,
I'm having trouble with debugging a PHP project through NetBeans using XDebug, and was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.