0

I was writing the script to display the social share counters of various API's and while running the PHP file it gives a Parse error: syntax error, unexpected T_FUNCTION. I know it's an older PHP version issue, as mine is 5.2.17 but I need suggestions to overcome this thing. Here's the code:

// Facebook
 array(
'name' => 'facebook',
'method' => 'GET',
'url' => 'https://graph.facebook.com/fql?q=' . urlencode("SELECT like_count, total_count, share_count, click_count, comment_count FROM link_stat WHERE url = \"{$url}\""),
'callback' => function($resp) {
        if(isset($resp->data[0]->total_count)) {
            return (int)$resp->data[0]->total_count;
        } else {
            return 0;
        }
})
  • 5
    PHP before 5.3 doesn't support anonymous functions – elclanrs Nov 27 '13 at 23:01
  • That's what my question asks about, is there any solution to overcome this? – Abhisek Das Nov 27 '13 at 23:03
  • I don't have experience with the facebook api, but you could probably just use `create_function` still if you want to use an anonymous function. Or pass the name of a function you define. – Jonathan Kuhn Nov 27 '13 at 23:04
  • 1
    Duplicates, http://stackoverflow.com/questions/3809405/converting-code-with-anonymous-functions-to-php-5-2, http://stackoverflow.com/questions/6412032/php-anonymous-function-causes-syntax-error-on-some-installations, http://stackoverflow.com/questions/8236452/convert-php-5-3-anonymous-function-into-5-2-compatible-function, among many others. – elclanrs Nov 27 '13 at 23:24

1 Answers1

0

Anonymous functions were introduced in PHP 5.3, so that syntax won't work with your PHP version. Try defining the function first and then passing the name of the function into the array.

function handle_response($resp) 
{
    if(isset($resp->data[0]->total_count)) {
        return (int)$resp->data[0]->total_count;
    } else {
        return 0;
    }
}

array(
    'name' => 'facebook',
    'method' => 'GET',
    'url' => 'https://graph.facebook.com/fql?q=' . urlencode("SELECT like_count, total_count, share_count, click_count, comment_count FROM link_stat WHERE url = \"{$url}\""),
    'callback' => 'handle_response'
)
Divey
  • 1,699
  • 1
  • 12
  • 22