【问题标题】:Unexpected T_FUNCTION when using 'callback' => function($resp) [duplicate]使用“回调”=> 函数($resp)时出现意外的 T_FUNCTION [重复]
【发布时间】:2013-11-27 23:00:35
【问题描述】:

我正在编写脚本来显示各种 API 的社交共享计数器,并且在运行 PHP 文件时它给出了 Parse error: syntax error, unexpected 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;
        }
})

【问题讨论】:

标签: javascript php jquery facebook


【解决方案1】:

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

【讨论】:

  • 尝试通过首先定义函数来做到这一点,但仍然显示相同的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 2012-12-02
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 2018-09-13
相关资源
最近更新 更多