Sorting by facebook likes
duhol Offline
Junior Member
**
Posts: 34
Joined: Mar 2011
Post: #1
Toungue Sorting by facebook likes
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

(This post was last modified: 10-13-2011 07:21 AM by duhol.)
10-13-2011 06:03 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: Sorting by facebook likes
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.

My Blog
10-13-2011 12:11 PM
Find all posts by this user Quote this message in a reply
duhol Offline
Junior Member
**
Posts: 34
Joined: Mar 2011
Post: #3
RE: Sorting by facebook likes
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?
10-13-2011 01:06 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #4
RE: Sorting by facebook likes
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.

My Blog
10-14-2011 08:06 AM
Find all posts by this user Quote this message in a reply
duhol Offline
Junior Member
**
Posts: 34
Joined: Mar 2011
Post: #5
RE: Sorting by facebook likes
I make a task

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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
(This post was last modified: 10-14-2011 09:33 AM by duhol.)
10-14-2011 09:27 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #6
RE: Sorting by facebook likes
(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.

My Blog
10-14-2011 12:11 PM
Find all posts by this user Quote this message in a reply
duhol Offline
Junior Member
**
Posts: 34
Joined: Mar 2011
Post: #7
RE: Sorting by facebook likes
I change task

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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

(This post was last modified: 10-15-2011 07:34 PM by duhol.)
10-15-2011 07:34 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #8
RE: Sorting by facebook likes
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.

My Blog
(This post was last modified: 10-15-2011 08:54 PM by ZiNgA BuRgA.)
10-15-2011 08:54 PM
Find all posts by this user Quote this message in a reply
duhol Offline
Junior Member
**
Posts: 34
Joined: Mar 2011
Post: #9
RE: Sorting by facebook likes
Ok. I got it.
10-16-2011 05:05 AM
Find all posts by this user Quote this message in a reply
duhol Offline
Junior Member
**
Posts: 34
Joined: Mar 2011
Post: #10
RE: Sorting by facebook likes

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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.

10-16-2011 06:01 AM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: