Danny620 Posted May 29, 2012 Share Posted May 29, 2012 <?php if ( !function_exists( 'curl_init' ) ) { die( "Social News Office needs the CURL PHP extension." ); } if ( !function_exists( 'json_decode' ) ) { die( "Social News Office needs the JSON PHP extension" ); } class SocialNewsOffice { private $meta_title; private $meta_description; private $website_url; private $permalink; private $api_url = 'http://www.socialnewsoffice.com/'; // Social News Office API URL private $api_key; private $request; private $data; private $cache_expire = 5; // Cache Refresh in Minutes (1 - 60) private $cache_folder_location; // Where to store Cache Folder private $header = false; // DISPLAY HEADERS (FALSE OR TRUE) private $follow = true; // FOLLOW REDIRCETS (FALSE OR TRUE) private $ssl = false; // If set false (it accpets any ssl) should false private $ctimeout = 5; // Timeout for connect in SECs when curl does next url private $timeout = 60; // Timeout of retriving page in SECs when curl does next url private $google_analytics = NULL; // Should contain values like (UA-28433510-1) private $facebook_fanpage_link = NULL; // Should contain values like (https://www.facebook.com/northplanet) private $youtube_username = NULL; // Should contain values like (northplanet) private $twitter_username = NULL; // Should contain values like (northplanet) private $google_plus = NULL; // Should contain values like (105665893715911819363) private $youtube_video = NULL; // Should contain values like (ZWoFHBwxk8o) private $disqus_shortname = NULL; // Should contain values like (northplanet) public function __construct( $api_key, $cache_expire_in_minutes, $cache_folder_location, $website_url ) { $this->api_key = $api_key; $this->cache_expire = $cache_expire_in_minutes; $this->cache_folder_location = $cache_folder_location; $settings = $this->getSettings(); $this->meta_title = $settings['0']->meta_title; $this->meta_description = $settings['0']->meta_description; $this->website_url = $website_url; $this->google_analytics = $settings['0']->google_analytics; $this->facebook_fanpage_link = $settings['0']->facebook_fanpage_link; $this->youtube_username = $settings['0']->youtube_username; $this->twitter_username = $settings['0']->twitter_username; $this->google_plus = $settings['0']->google_plus; $this->youtube_video = $settings['0']->youtube_video; $this->disqus_shortname = $settings['0']->disqus_shortname; } public function setHeader( $header ) { $this->header = $header; } public function setFollow( $follow ) { $this->follow = $follow; } public function setSsl( $ssl ) { $this->ssl = $ssl; } public function setCtimeout( $ctimeout ) { $this->ctimeout = $ctimeout; } public function setTimeout( $timeout ) { $this->timeout = $timeout; } private function connect( ) { $ch = curl_init(); $con = $this->api_url . $this->request . '&api_key=' . $this->api_key; curl_setopt( $ch, CURLOPT_URL, $con ); curl_setopt( $ch, CURLOPT_HEADER, $this->header ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, $this->follow ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, $this->ssl ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout ); curl_setopt( $ch, CURLOPT_TIMEOUT, $this->timeout ); $response = curl_exec( $ch ); /* Check for 404 (file not found). */ $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); if ( $httpCode == 404 ) { return '404'; } else { return $response; } curl_close( $ch ); } public function cacheApiResult( ) { $cache_file = $this->cache_folder_location . $this->request . '.json'; $expires = time() - ( 60 * $this->cache_expire ); if ( !file_exists( $cache_file ) ) { $handle = fopen( $cache_file, 'w' ); fclose( $handle ); } // Check that the file is older than the expire time and that it's not empty if ( filectime( $cache_file ) < $expires || file_get_contents( $cache_file ) == '' ) { // File is too old, refresh cache $api_result = $this->connect(); try { if ( $api_result == '404' ) { throw new Exception( "API: Page Not Found - 404" ); // Remove cache file on error to avoid writing wrong json unlink( $cache_file ); } else { file_put_contents( $cache_file, json_encode( $api_result ) ); $this->data = json_decode( file_get_contents( $cache_file ) ); } } catch ( Exception $e ) { echo 'Error ' . $e->getMessage(); } } else { // Fetch cache $this->data = json_decode( file_get_contents( $cache_file ) ); } } public function getWebsiteURL() { return $this->website_url; } public function getPermalink($post_url) { $this->permalink = $this->website_url . 'post/' . $post_url; return $this->permalink; } public function getThumbnail($img_url, $w=250,$h=190,$mode='fit') { return $this->website_url . 'sno-blog/src.php?src=' . $img_url . "&w=$w&h=$h&mode=$mode"; } public function getMetaTitle() { return $this->meta_title; } public function getMetaDescription() { return $this->meta_description; } public function getGoogleAnalytics() { return $this->google_analytics; } public function getFacebookFanpageLink() { return $this->facebook_fanpage_link; } public function getYoutubeUsername() { return $this->youtube_username; } public function getTwitterUsername() { return $this->twitter_username; } public function getGooglePlus() { return $this->google_plus; } public function getYoutubeVideo() { return $this->youtube_video; } public function getDisqusShortName() { return $this->disqus_shortname; } public function getBlogPosts( $start = 0, $display = 6, $categorie = NULL ) { if ( $start == '' ) { $start = 0; } if ( $display == '' ) { $display = 6; } $this->request = "blog_posts?start=$start&display=$display&categorie=$categorie"; $this->cacheApiResult(); return json_decode( $this->data ); } public function getPost( $blog_url = '' ) { $this->request = "post?blog_url=$blog_url"; $this->cacheApiResult(); $post = json_decode( $this->data ); if(isset($post[0]->post_title)){ $this->meta_title = substr(strip_tags($post[0]->post_title), 0, 65); } if(isset($post[0]->post_content)){ $this->meta_description = substr(strip_tags($post[0]->post_content), 0, 150); } return $post; } public function getCategories() { $this->request = "categories?"; $this->cacheApiResult(); return json_decode( $this->data ); } public function getFeaturedNews() { $this->request = "featured_news?"; $this->cacheApiResult(); return json_decode( $this->data ); } public function getSettings() { $this->request = 'settings?'; $this->cacheApiResult(); return json_decode( $this->data ); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263313-improve-suggestions-welcome/ Share on other sites More sharing options...
Ivan Ivković Posted May 29, 2012 Share Posted May 29, 2012 Well.. Obviously you didn't state any problem you need help with. Quote Link to comment https://forums.phpfreaks.com/topic/263313-improve-suggestions-welcome/#findComment-1349453 Share on other sites More sharing options...
ignace Posted May 29, 2012 Share Posted May 29, 2012 $settings['0'] is the same as $settings[0] In this code: $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); if ( $httpCode == 404 ) { return '404'; } else { return $response; } curl_close( $ch ); curl_close() is never executed. Same here: throw new Exception( "API: Page Not Found - 404" ); // Remove cache file on error to avoid writing wrong json unlink( $cache_file ); unlink() is never executed. Quote Link to comment https://forums.phpfreaks.com/topic/263313-improve-suggestions-welcome/#findComment-1349460 Share on other sites More sharing options...
Danny620 Posted May 29, 2012 Author Share Posted May 29, 2012 How about now? <?php if ( !function_exists( 'curl_init' ) ) { die( "Social News Office needs the CURL PHP extension." ); } if ( !function_exists( 'json_decode' ) ) { die( "Social News Office needs the JSON PHP extension" ); } class SocialNewsOffice { private $meta_title; private $meta_description; private $website_url; private $permalink; private $api_url = 'http://www.socialnewsoffice.com/'; // Social News Office API URL private $api_key; private $request; private $data; private $cache_expire = 5; // Cache Refresh in Minutes (1 - 60) private $cache_folder_location; // Where to store Cache Folder private $header = false; // DISPLAY HEADERS (FALSE OR TRUE) private $follow = true; // FOLLOW REDIRCETS (FALSE OR TRUE) private $ssl = false; // If set false (it accpets any ssl) should false private $ctimeout = 5; // Timeout for connect in SECs when curl does next url private $timeout = 60; // Timeout of retriving page in SECs when curl does next url private $google_analytics = NULL; // Should contain values like (UA-28433510-1) private $facebook_fanpage_link = NULL; // Should contain values like (https://www.facebook.com/northplanet) private $youtube_username = NULL; // Should contain values like (northplanet) private $twitter_username = NULL; // Should contain values like (northplanet) private $google_plus = NULL; // Should contain values like (105665893715911819363) private $youtube_video = NULL; // Should contain values like (ZWoFHBwxk8o) private $disqus_shortname = NULL; // Should contain values like (northplanet) public function __construct( $api_key, $cache_expire_in_minutes, $cache_folder_location, $website_url ) { $this->api_key = $api_key; $this->cache_expire = $cache_expire_in_minutes; $this->cache_folder_location = $cache_folder_location; $settings = $this->getSettings(); $this->meta_title = $settings[0]->meta_title; $this->meta_description = $settings[0]->meta_description; $this->website_url = $website_url; $this->google_analytics = $settings[0]->google_analytics; $this->facebook_fanpage_link = $settings[0]->facebook_fanpage_link; $this->youtube_username = $settings[0]->youtube_username; $this->twitter_username = $settings[0]->twitter_username; $this->google_plus = $settings[0]->google_plus; $this->youtube_video = $settings[0]->youtube_video; $this->disqus_shortname = $settings[0]->disqus_shortname; } public function setHeader( $header ) { $this->header = $header; } public function setFollow( $follow ) { $this->follow = $follow; } public function setSsl( $ssl ) { $this->ssl = $ssl; } public function setCtimeout( $ctimeout ) { $this->ctimeout = $ctimeout; } public function setTimeout( $timeout ) { $this->timeout = $timeout; } private function connect( ) { $ch = curl_init(); $con = $this->api_url . $this->request . '&api_key=' . $this->api_key; curl_setopt( $ch, CURLOPT_URL, $con ); curl_setopt( $ch, CURLOPT_HEADER, $this->header ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, $this->follow ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, $this->ssl ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout ); curl_setopt( $ch, CURLOPT_TIMEOUT, $this->timeout ); $response = curl_exec( $ch ); /* Check for 404 (file not found). */ $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); curl_close( $ch ); if ( $httpCode == 404 ) { return '404'; } else { return $response; } } public function cacheApiResult( ) { $cache_file = $this->cache_folder_location . $this->request . '.json'; $expires = time() - ( 60 * $this->cache_expire ); if ( !file_exists( $cache_file ) ) { $handle = fopen( $cache_file, 'w' ); fclose( $handle ); } // Check that the file is older than the expire time and that it's not empty if ( filectime( $cache_file ) < $expires || file_get_contents( $cache_file ) == '' ) { // File is too old, refresh cache $api_result = $this->connect(); try { if ( $api_result == '404' ) { // Remove cache file on error to avoid writing wrong json unlink( $cache_file ); throw new Exception( "API: Page Not Found - 404" ); } else { file_put_contents( $cache_file, json_encode( $api_result ) ); $this->data = json_decode( file_get_contents( $cache_file ) ); } } catch ( Exception $e ) { echo 'Error ' . $e->getMessage(); } } else { // Fetch cache $this->data = json_decode( file_get_contents( $cache_file ) ); } } public function getWebsiteURL() { return $this->website_url; } public function getPermalink($post_url) { $this->permalink = $this->website_url . 'post/' . $post_url; return $this->permalink; } public function getThumbnail($img_url, $w=250,$h=190,$mode='fit') { return $this->website_url . 'sno-blog/src.php?src=' . $img_url . "&w=$w&h=$h&mode=$mode"; } public function getMetaTitle() { return $this->meta_title; } public function getMetaDescription() { return $this->meta_description; } public function getGoogleAnalytics() { return $this->google_analytics; } public function getFacebookFanpageLink() { return $this->facebook_fanpage_link; } public function getYoutubeUsername() { return $this->youtube_username; } public function getTwitterUsername() { return $this->twitter_username; } public function getGooglePlus() { return $this->google_plus; } public function getYoutubeVideo() { return $this->youtube_video; } public function getDisqusShortName() { return $this->disqus_shortname; } public function getBlogPosts( $start = 0, $display = 6, $categorie = NULL ) { if ( $start == '' ) { $start = 0; } if ( $display == '' ) { $display = 6; } $this->request = "blog_posts?start=$start&display=$display&categorie=$categorie"; $this->cacheApiResult(); return json_decode( $this->data ); } public function getPost( $blog_url = '' ) { $this->request = "post?blog_url=$blog_url"; $this->cacheApiResult(); $post = json_decode( $this->data ); if(isset($post[0]->post_title)){ $this->meta_title = substr(strip_tags($post[0]->post_title), 0, 65); } if(isset($post[0]->post_content)){ $this->meta_description = substr(strip_tags($post[0]->post_content), 0, 150); } return $post; } public function getCategories() { $this->request = "categories?"; $this->cacheApiResult(); return json_decode( $this->data ); } public function getFeaturedNews() { $this->request = "featured_news?"; $this->cacheApiResult(); return json_decode( $this->data ); } public function getSettings() { $this->request = 'settings?'; $this->cacheApiResult(); return json_decode( $this->data ); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263313-improve-suggestions-welcome/#findComment-1349501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.