Twilio api 解析错误:意外T_CONST,预期T_OLD_FUNCTION或T_FUNCTION或T_VAR或


Twilio api parse error: unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}'

我有PHP 5.4.21

我正在使用在浏览器中正常工作的 Twilio API,但在命令行模式下,我收到以下错误:

<b>Parse error</b>:  syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in <b>sms/Services/Twilio.php</b> on line <b>36</b>
指定文件的第 36 行

是以下类的一部分,请参阅我指定第 36 行的注释。

我根据一些初始反馈请求添加了第 36 行之前和之后的所有代码行作为上下文,以显示这些行。

<?php
function Services_Twilio_autoload($className) {
    if (substr($className, 0, 15) != 'Services_Twilio') {
        return false;
    }
    $file = str_replace('_', '/', $className);
    $file = str_replace('Services/', '', $file);
    return include dirname(__FILE__) . "/$file.php";
}
spl_autoload_register('Services_Twilio_autoload');
/**
 * Create a client to talk to the Twilio API.
 *
 *
 * :param string               $sid:      Your Account SID
 * :param string               $token:    Your Auth token from
 *      twilio.com/user/account
 * :param string               $version:  API version to use
 * :param $_http:    A HTTP client for making requests.
 * :type $_http: :php:class:`Services_Twilio_Http`
 * :param int                  $retryAttempts:
 *      Number of times to retry failed requests. Currently only idempotent
 *      requests (GET's and DELETE's) are retried .
 */
class Services_Twilio extends Services_Twilio_Resource
{
    /* this is Line 36... */
    const USER_AGENT = 'twilio-php/3.12.0';
    protected $http;
    protected $retryAttempts;
    protected $last_response;
    protected $version;
    protected $versions = array('2008-08-01', '2010-04-01');
    public function __construct(
        $sid,
        $token,
        $version = null,
        Services_Twilio_TinyHttp $_http = null,
        $retryAttempts = 1
    ) {
              $_http = new Services_Twilio_TinyHttp(
                  "https://api.twilio.com",
                  array("curlopts" => array(
                      CURLOPT_USERAGENT => self::USER_AGENT,
                      CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
                      CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
                  ))
              );
    }
}
?>

来自我最初的评论:

据我所知,最可能的解释是您的命令行正在运行与您的 Web 服务器不同版本的 PHP。尝试在命令行上运行 php -v 以确认它正在运行您期望的版本。

OP的回应

@Spudley,感谢您的反馈。似乎你是唯一一个真正阅读我的帖子而不做假设的人。正如你所说,我运行了"php -v",这就是我得到的,所以很明显我正在运行早期版本的 PHP: PHP 4.4.9 (cgi-fcgi) (构建时间: Jul 22 2013 09:48:43) 版权所有 (c) 1997-2008 PHP 组 Zend Engine v1.3.0, 版权所有 (c) 1998-2004 Zend Technologies

所以我们开始了,这显然是问题所在。你有 PHP 4.4 在那里。这需要去。

解决方案摆脱旧的PHP版本,并确保您的命令行php运行与Web服务器相同的版本。

尚未指定正在运行的操作系统,这显然会对您卸载的方式产生重大影响。

如果你能在卸载工具中找到旧的PHP版本,那就去做吧,希望你能开始神奇地拿起较新的PHP版本。如果没有,那么根据配置方式,您可能会发现最简单的方法是卸载两个PHP版本,然后重新安装5.4版本。