Exemple #1
0
function get_url($url)
{
    $content = '';
    $cur_time = time();
    $filename = "requests/" . md5($url) . ".json";
    //echo $filename;
    $getNew = FALSE;
    if (file_exists($filename)) {
        //echo '<br>file exists';
        $file = basename($filename, ".json");
        $old_time = filemtime($filename);
        //echo '<br> Prev time: '.$old_time;
        $mins = $cur_time - $old_time;
        //echo " -- ".$mins;
        if ($mins >= 300) {
            $getNew = TRUE;
        } else {
            $content = file_get_contents($filename);
        }
    } else {
        //echo '<br>file does not exists';
        $getNew = TRUE;
    }
    if ($getNew) {
        //echo '<br> getting new';
        $needLogin = TRUE;
        if ($needLogin) {
            //echo '<br>need login';
            $login_url = 'http://www.reddit.com/api/login/username';
            $curl = new mycurl();
            $curl->setCookiFileLocation('requests/cookie.txt');
            $data = array('api_type' => 'json', 'user' => 'ReillyKlevre', 'passwd' => 'password');
            $curl->setPost($data);
            $curl->createCurl($login_url);
            if ($curl->getHttpStatus() != 200) {
                echo '<br>LOGIN ERROR > HTTP STATUS: ' . $curl->getHttpStatus();
                exit;
            }
        }
        //echo '<br>after: '.$url;
        $curl = new mycurl();
        $curl->setCookiFileLocation('requests/cookie.txt');
        $curl->createCurl($url);
        if ($curl->getHttpStatus() != 200) {
            echo '<br>REDDIT_URL ' . $url . ' > HTTP STATUS: ' . $curl->getHttpStatus();
            exit;
        }
        $content = $curl->__tostring();
        if ($content) {
            $fp = fopen($filename, 'w');
            fwrite($fp, $content);
            fclose($fp);
        }
    }
    return $content;
}