vForums Support > vForums :: Support :: > stream to website

stream to website - Posted By Mark (markwesley) on 14th Apr 09 at 12:05pm
Forum URL: markawes.vforums.co.il
Experienced By: Mark Me Hi
Browser: Fire fox 3 {Smile}
Operating System: Windows Vista Home Premium Edition yeah {Tongue Out}

Problem: Are we allowed to have a board on our forum like you have that streams information to our website? apparently PB dont allow this so i was wondering if you do?

Re: stream to website - Posted By Mark (markwesley) on 14th Apr 09 at 12:12pm
See now i have the API thing enabled on my forum but never knew what it did even though u put the wireless key to our hub as the code {Tongue Out} ill have a look but im guessing ill be back for help. {Smile}

Thank you for your quick reply.

Re: stream to website - Posted By Ross (admin) on 14th Apr 09 at 12:18pm
That is all done via the API interface

http://support.vforums.us/board/api

The API allows you to collect information (such as a news feed) and then output it however you want.

Re: stream to website - Posted By Mark (markwesley) on 14th Apr 09 at 12:45pm
ive had a look at the board an im compuzzled, do i copy the php script and upload it to my server?
http://support.vforums.us/board/api/topic/8740/action/view_topic/api-source-code

Re: stream to website - Posted By Nick (nickb) on 14th Apr 09 at 12:53pm
Sorry to jump in on this thread but... I thought it was vforums.co.uk not vforums.us {Confused}




and I am assuming that yeah you do have to upload to your own host.

Re: stream to website - Posted By Michael (wrighty) on 14th Apr 09 at 12:56pm
@ Darkmage - both work! {Smile}  

Re: stream to website - Posted By Nick (nickb) on 14th Apr 09 at 12:57pm
Ahh I see, So us means United States? I guess I learned something new this morning! {Cheesy}

Re: stream to website - Posted By Mark (markwesley) on 14th Apr 09 at 12:58pm
The php script cannot be found on the server when i try and download it from that topic

Re: stream to website - Posted By Ross (admin) on 14th Apr 09 at 1:37pm
Here's the php code which our news feed uses

Code:
 
  1. <?php
  2.     include_once('vforums_api.php'); // This is the "Source" file
  3.     $vf = new vforums_api('www.support.virtualforums.co.uk', 'API KEY / PASSWORD');
  4.     $news_items = $vf -> request("api/recent/board/BOARDID/max/3/allow_all/1");
  5.     $count = 0;
  6.     if(!is_array($news_items)) {
  7.         $news_items = Array();
  8.             $news_items[0] = Array(
  9.                 "description" => "",
  10.                 "subject" => "Support Forum/API Offline",
  11.                 "message" => "This website is unable to connect to the Support Forums API interface. This may be due to scheduled maintenance or a server error. We hope to have everything back online as soon as possible.",
  12.                 "timestamp" => time()
  13.             );
  14.     }
  15.     foreach($news_items as $news) {
  16.         $news_link = explode("|", $news['description']);
  17.         if($count > 0) {
  18. ?>
  19.                 <br />
  20.                 <br />
  21.                 <br />
  22. <?php
  23.         }
  24. ?>
  25.                 <h2 class="news_title"><?=htmlspecialchars($news['subject']);?></h2>
  26.                 <div class="news_body">
  27. <?=preg_replace("/&(\s)/", "& ", $news['message']);?>
  28.                 </div>
  29.                 <div class="news_info">
  30.                     <div class="news_date">
  31.                         Posted on: <?=gmdate("jS M Y", $news['timestamp']);?>
  32.                     </div>
  33. <?php
  34.         if(isset($news_link[1])) {
  35.             $link_prefix = preg_match("/(board\/|http:\/\/)/", $news_link[0])? (preg_match("/http:\/\//", $news_link[0])? "" : "http://support.virtualforums.co.uk/") : "http://support.virtualforums.co.uk/board/announcements/topic/";
  36. ?>
  37.                     <div class="news_link">
  38.                         Read More: <a href="<?=$link_prefix . $news_link[0];?>"><?=htmlspecialchars($news_link[1]);?></a>
  39.                     </div>
  40. <?php
  41.         }
  42. ?>
  43.                 </div>
  44. <?php
  45.         $count++;
  46.     }
  47. ?>
  48.  
 


Also, I have fixed the download link. And as pointed out, any of the domain names work (virtualforums.co.uk, vforums.co.uk, vforums.us)

Re: stream to website - Posted By Mark (markwesley) on 14th Apr 09 at 4:58pm
Thanks Ross, ive done this what i think i need to do is it correct?#

Code:
 
  1. <?php 
  2.  
  3. /****************
  4. vForums API Interface
  5. Release 0.0.1
  6. Updated 14.04.2008
  7. ****************/
  8.  
  9.     class vforums_api { 
  10.         var $api_forum; 
  11.         var $api_key; 
  12.         var $api_user; 
  13.         var $api_pass; 
  14.          
  15.         function vforums_api($forum, $key) { 
  16.             if($forum && $key) { 
  17.                 $this -> api_key = $this -> encode($key); 
  18.                 $this -> api_forum = str_replace('http://', '', $forum); 
  19.                 // Fix common errors 
  20.                 ini_set('memory_limit', '128MB'); 
  21.                 ini_set('pcre.backtrack_limit', 100000000); 
  22.                 ini_set('pcre.recursion_limit', 100000000); 
  23.                 return true; 
  24.             } 
  25.             return false; 
  26.         } 
  27.  
  28.         function encode($str) { 
  29.             return urlencode($str); 
  30.         } 
  31.      
  32.         function login_user($user = 'www.markawes.vforums.co.uk', $pass = '2caecb2ce2') { 
  33.             // Validate & Login User 
  34.             if(isset($_POST[$user]) && isset($_POST[$pass])) { 
  35.                 $this -> api_user = $this -> encode($_POST[$user]); 
  36.                 $this -> api_pass = $this -> encode($_POST[$pass]); 
  37.                 $encrypted = 0; 
  38.             } else { 
  39.                 $this -> api_user = (isset($_COOKIE[$user]))? $this -> encode($_COOKIE[$user]) : false;  
  40.                 $this -> api_pass = (isset($_COOKIE[$pass]))? $this -> encode($_COOKIE[$pass]) : false;  
  41.                 $encrypted = 1; 
  42.             } 
  43.             if($this -> api_user && $this -> api_pass) { 
  44.                 $user_information = $this -> request("api/profile/encrypted/$encrypted"); 
  45.                 if(isset($user_information['password'])) { 
  46.                     if(!$encrypted) {             
  47.                         $login_time = time() + (60 * 60 * 24); 
  48.                         $password = addslashes($user_information['password']); 
  49.                         $username = addslashes($user_information['user_name']); 
  50.                         header("Set-Cookie: $pass=$password; expires=". date("D, d-M-Y H:i:s T", $login_time) ."; path=/; HTTPonly;", false); 
  51.                         header("Set-Cookie: $user=$username; expires=". date("D, d-M-Y H:i:s T", $login_time) ."; path=/;", false); 
  52.                     } 
  53.                     return $user_information; 
  54.                 }
  55.                 $this -> api_user = $this -> api_pass = false; 
  56.             } 
  57.             return false; 
  58.         } 
  59.          
  60.         function request($request) { 
  61.             if(!$this -> api_forum || !$this -> api_key) { 
  62.                 return "vForums API has not been initiated"; 
  63.             } 
  64.             if(is_array($request)) { 
  65.                 // Construct URL from array \w URL encoding 
  66.                 $request_temp = ""; 
  67.                 foreach($request as $r) { 
  68.                     if(!empty($request_temp)) 
  69.                         $request_temp .= "/"; 
  70.                     $request_temp .= $this->encode($r); 
  71.                 } 
  72.                 $request = $request_temp; 
  73.             } 
  74.             $request .= "/key/" . urlencode($this -> api_key); 
  75.             if($this -> api_user) { 
  76.                 $request .= "/username/" . $this -> api_user; 
  77.             } 
  78.             if($this -> api_pass) { 
  79.                 $request .= "/password/" . $this -> api_pass; 
  80.             } 
  81.  
  82.             // Post to Server 
  83.             $header = "GET /$request HTTP/1.0\r\n"; 
  84.             $header .= "HOST: ".$this -> api_forum."\r\n"; 
  85.             $header .= 'Content-Type: application/x-www-form-urlencoded\r\n'; 
  86.             $header .= "Content-Length: " . strlen($request) . "\r\n\r\n"; 
  87.             $fp = fsockopen ($this -> api_forum, 80, $errno, $errstr, 30); 
  88.             if (!$fp) { 
  89.                 return "Could not initiate connection to the forum."; 
  90.             } else { 
  91.                 fputs ($fp, $header); 
  92.                 $headerdone = false; 
  93.                 $data = ''; 
  94.                 while (!feof($fp)) { 
  95.                     $line = fgets ($fp, 1024); 
  96.                     if (strcmp($line, "\r\n") == 0) { 
  97.                         $headerdone = true; 
  98.                     } else if ($headerdone) { 
  99.                         $data .= $line; 
  100.                     } 
  101.                 } 
  102.             }
  103.             // Construct Result 
  104.             $results = unserialize($data);
  105.             if(empty($results))
  106.                     return $data;
  107.             return $results;
  108.         } 
  109.     } 
  110.      
  111. ?>  
 

Re: stream to website - Posted By Mark (markwesley) on 16th Apr 09 at 1:08pm
bump

Re: stream to website - Posted By Ross (admin) on 16th Apr 09 at 2:19pm
Hi Mark.

That code is what I'd recommend as saving and uploading as a seperate file to your server. I tend to always call it "vforums_api.php" as shown in the news example. By keeping this code as a seperate file you can use that same core code for multiple sections and if you ever need to update it then it is all in one place.

The news feed example I posted then uses this vforums_api.php file and uses the code within it to connect to your forum and fetch the requested information.