简体   繁体   中英

PHP calling external scripts

I am creating an activity feed with PHP for individual and I'm moving on to creating group feeds.

so I have tables of

Table: Group Name 1. Group 1 2. Group 2 3. Group 3

Table: Members 1. Group 1 > Member 1, Member 2, Member 3, Member 4, Member 5 2. Group 2 > Member 3, Member 4, Member 5, Member 5....

Some groups have thousands of members. So when ever I publish a feed, I am doing something like the following.

$result = $this->db->query('SELECT member_id FROM members WHERE group_id = 1');
$members = $result->fetchAll();

foreach ($members as $member) {
$query = $this->db->prepare('INSERT INTO activity_feed (user_id, activity) VALUES(:user_id, :activity);

if ($result = $query->execute(array(
':user_id' => $member['member_id'],
':activity' => $activity
))) {
//do something
 $pusher->push(activity) //socket push notification
}
}

Inserting of personal feed only requires one mysql database insert, whereelse As you can see, for group feed the loop gets exponentially larger when the number of members increases.

I was thinking of populating the activity feed using a separate PHP file, and execute it when the activity occurs. How do I call that separate PHP file?

Is there a better idea to solve this problem?

If not I have to go through the loop everytime someone post a message to the group, or upload image to the group and then the PHP file takes along time to execute.

You can put your PHP code into a separate file, say dbnotify.php and then call it using exec() :

<?php
$group = 1;
$activity = "foo";
exec("/path/to/dbnotify.php $group $activity &");

Note the & after the command, this puts it in the background and returns immediately.

Now in your external script, you want to read the values you passed on the command line and put them in the query. In addition, you can probably improve this database performance a lot by making the insert a single query:

#!/usr/bin/php
<?php
$groupid = $argv[1];
$activity = $argv[2];


$query = $this->db->prepare("INSERT INTO activity_feed (user_id, activity) SELECT member_id, :activity FROM members WHERE group_id = :group_id");
$result = $query->execute(array(":activity"=>$activity, ":group_id"=>$groupid));


$result = $this->db->query('SELECT member_id FROM members WHERE group_id = 1');
$members = $result->fetchAll();
foreach ($members as $member) {
    $pusher->push(activity); //socket push notification
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM