Пример #1
0
/**
 * creates the method_feed json encoded string which is used for batch calling facebook
 *
 * @param array $methods 
 * @param array $params 
 * @return string 
 * @author Craig Ulliott
 */
function create_fb_batch_method_feed(array $request_collection)
{
    if (count($request_collection) > 20) {
        throw new exception('facebook allows a maximum 20 requests per batch');
    }
    $method_feed = array();
    // create a signed request for every method
    while ($request = array_shift($request_collection)) {
        $method_feed[] = build_fb_query_string($request['method'], $request['params'], true);
    }
    return json_encode($method_feed);
}
Пример #2
0
 /**
  * test the helper function to get sprites
  */
 public function test_build_fb_query_string()
 {
     $post_fields = array();
     build_fb_query_string('facebook.users.getInfo', $post_fields, $combine = false);
 }
Пример #3
0
 /**
  * Send an email to multiple facebook users at once, splits them between batch calls and curl threads to achieve the fastest possible transfer
  *
  * @param array $facebookIDs 
  * @param string $subject 
  * @param string $text 
  * @param string $html 
  * @return bool
  * @author Craig Ulliott
  */
 public static function sendBulkNetworkEmail(array $email_collection)
 {
     // we create a collection of requests to the notifications.sendEmail API
     $request_collection = array();
     // we build queries to the batch.run api call, containing upto FB_MAX_BATCH_COUNT requests
     $request_strings_collection = array();
     // build a request out of each email
     foreach ($email_collection as $email) {
         $request_collection[] = array('method' => 'facebook.notifications.sendEmail', 'params' => array('recipients' => $email['facebookID'], 'subject' => $email['subject'], 'text' => $email['text'], 'fbml' => $email['fbml']));
     }
     // as only 4 calls can be added to a batch, so we spread sets of FB_MAX_BATCH_COUNT calls over multiple threads
     $required_threads = ceil(count($request_collection) / 5);
     // break it into groups of 4, the last one can have less than 4, array_split is safe used this way
     for ($i = 0; $i < $required_threads; $i++) {
         // the group of FB_MAX_BATCH_COUNT requests
         $thread_request_collection = array_slice($request_collection, 5 * $i, 5);
         $method_feed_string = create_fb_batch_method_feed($thread_request_collection);
         $request_strings_collection[] = build_fb_query_string('facebook.batch.run', array('method_feed' => $method_feed_string));
     }
     // call facebook in a multithreaded way
     $multi_results = call_fb_multi($request_strings_collection);
     // combine all the results
     $sent = array();
     // for each thread we used
     foreach ($multi_results as $results) {
         // expecting an array of results, as we were calling a batch method
         if (is_array($results)) {
             foreach ($results as $result) {
                 // these results were json encoded strings, trim is faster than json_decode
                 $result = trim($result, '"');
                 if ($result) {
                     $sent[] = $result;
                 }
             }
         }
     }
     return $sent;
 }