MyBB Hacks

Full Version: Sorting by facebook likes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi

I write about my problem at this forum because there are two specialists who help me Winktongue

Well, it about facebook like, again

What did I do until now.
1. I create row in table mybb_threads in database called "fblikes" same as "views".
2. Php code shows me number of likes in thread:

PHP Code:
$source_url[] = "http://www.sentens.pl/showthread.php?tid={$tid}";
$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".implode( ',', $source_url );
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);
$totals = $xml->link_stat->total_count;

where $totals is requested number of likes.

And now. What is need to do to write number $totals into database cell "fblikes"?



Only thing that I dont know how to do is write $totals into database.
Or there is a way to use xthreads???

So, PHP Gurus, will you help? Smile

Sorting threads by # of likes?
If you're going to rely on an external service, I think you've really got to think of the performance impact and how you wish to generate updates (i.e. staggered based on task?).  In which case, you'd commit the data when the update is triggered.
Maybe, it is possible to make a task that read tid, taking data from fb for this tid and put it into table? Will it works?
Why are you making a new table?
What is the $tid variable doing?

I think you really should think through what you're writing.
Actually, before you even do that, take the time and think through the design properly and how you're going to architect this, and why.
I make a task

PHP Code:
<?php
{
	global $mybb, $db, $lang;
	$threads = array();
	// Update thread likes
	$query = $db->query("
		SELECT tid
		FROM ".TABLE_PREFIX."threads
		GROUP BY tid
	");
	while($thread = $db->fetch_array($query))
	{
			$source_url[] = "http://www.sentens.pl/showthread.php?tid=".$thread['tid'];
			$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".implode( ',', $source_url );
			$xml = file_get_contents($url);
			$xml = simplexml_load_string($xml);

			foreach( $xml -> link_stat as $l)
			{
				$total = $l -> total_count;
				$db->update_query("threads", array('likes' => "{$total}"), "tid='{$thread['tid']}'", 1, true);
			}
	}
}
?>


but it take much time for 1000 threads. Is there a way to optimize it?


(10-14-2011 08:06 AM)ZiNgA BuRgA Wrote: [ -> ]Why are you making a new table?
What is the $tid variable doing?

I think you really should think through what you're writing.
Actually, before you even do that, take the time and think through the design properly and how you're going to architect this, and why.

I dont know how to coding. I learn when I want to make something. Dont be so rough for me Erf
(10-14-2011 09:27 AM)duhol Wrote: [ -> ]I dont know how to coding. I learn when I want to make something. Dont be so rough for me Erf
For something like this, that strategy will simply result in tears.

You can't expect to understand rocket science simply by attempting to build a rocket and learning as you go.  You really need to understand all the underlying theory beforehand (which will then allow you to come up with a design etc).
Similarly, something like this needs to be designed.  It's great that you're willing to learn, however, you can't simply just jump in and hope you'll get somewhere.

Although here you don't really need to understand coding to design it, but you need to come up with a design on how you want this to exactly work from a technical standpoint.  You know that fetching from an external server is slow, so how do you wish to implement this fetch?
If the service supports supplying multiple URLs at once, then you need to consider if you can send multiple URLs at a time to get around the fact that sending thousands of requests is just simply slow.
I change task

PHP Code:
<?php
    global $mybb, $db, $lang;
    $threads = array();
    // Update thread likes
    $query = $db->query("
        SELECT tid
        FROM ".TABLE_PREFIX."threads
        GROUP BY tid
    ");
    while($thread = $db->fetch_array($query))
    {
            $source_url[] = "http://www.sentens.pl/showthread.php?tid=".$thread['tid'];
            $url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".implode( ',', $source_url );
            $xml = file_get_contents($url);
            $xml = simplexml_load_string($xml);

    }

            foreach( $xml -> link_stat as $l)
            {
                $s = $l -> url;
                $out = preg_replace("#[^0-9]*#" ,"", $s);
                $l -> total_count;
                $total = $l -> total_count;
                $db->update_query("threads", array('likes' => "{$total}"), "tid='{$out}'", 1, true);

            }

?>


but the problem is that facebook restserver has trouble with 1000 threads at once. My task ask him about all and then write data to DB.

Can you help me to change a task in way that it ask about first thread, then write data to table, ask about second thread and write data to table, ask about third thread and write data to table, etc.

Thanks

No, I'm not going to write the whole thing for you.

I recommend you read through your code and make sure you're able to explain exactly what each line does.
Also, go through a few loop iterations (from your query) and explain to me the state of each variable after executing each line of code.
Ok. I got it.

PHP Code:
<?php
    global $mybb, $db, $lang;
    $threads = array();
    // Update thread likes
    $query = $db->query("
        SELECT tid
        FROM ".TABLE_PREFIX."threads
        GROUP BY tid LIMIT 0, 10;
    ");
    while($thread = $db->fetch_array($query))
    {

            $url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=http://sentens.pl/showthread.php?tid=".$thread['tid'];
			$numbers = file_get_contents($url);
			$total_count = ??????
			$db->update_query("threads", array('likes' => "{$total_count}"), "tid='{$thread['tid']}'", 1, true);
              
    }
?>


Is it good for now? I dont know how to get "total_count" from file content.

Pages: 1 2
Reference URL's