使用'时出现意外的T_FUNCTION;回调'=>函数($resp)

Unexpected T_FUNCTION when using 'callback' => function($resp)

本文关键字:函数 gt resp 回调 意外 使用 FUNCTION      更新时间:2023-12-09

我正在编写显示各种API的社交共享计数器的脚本,在运行PHP文件时,它给出了解析错误:语法错误,意外的T_FUNCTION。我知道这是一个旧的PHP版本问题,因为我的版本是5.2.17,但我需要一些建议来克服这个问题。这是代码:

// 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;
        }
})

匿名函数是在PHP 5.3中引入的,因此语法不适用于您的PHP版本。请尝试先定义函数,然后将函数的名称传递到数组中。

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'
)