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

StackOverflow https://stackoverflow.com/questions/19521749

Question

I have PHP 5.4.21

I'm using a Twilio API that works fine from a browser but in command line mode, I get the following error:

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

Line 36 of the specified file is part of the following class, please see my note where I specify Line 36.

I added all lines of code leading up to and immediately after Line 36 for context, based on some initial feedback requests to show these lines.

<?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',
                  ))
              );
    }
}
?>
Was it helpful?

Solution

From my original comment:

The most likely explanation as far as I can see is that your command-line is running a different version of PHP to your web server. Try running php -v at the commandline to confirm that it's running the version you expect.

OP's response

@Spudley, thank you for your feedback. Seems like you're the only one actually reading my post and not making assumptions. As you said, I ran "php -v" and here's what I get, so it's apparent I'm running an earlier version of PHP: PHP 4.4.9 (cgi-fcgi) (built: Jul 22 2013 09:48:43) Copyright (c) 1997-2008 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

So there we go, that's clearly the problem. You've got PHP 4.4 on there. That needs to go.

Solution get rid of the old busted version of PHP, and make sure your command line php is running the same version as your web server.

You haven't specified what operating system you're running, which will obviously make big a difference to how you do the un-install.

If you can find the old PHP version in your un-install tool, do it and hopefully you'll start picking up the newer PHP version by magic. If not, then depending on how things are configured you may find that the easiest wat to achieve this is to un-install both PHP versions, and then re-install the 5.4 version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top