Script error in PHP 5.3



  • There's a posting class in the vc, which is PHP 5.3.2 making a mistake on line 15:

    syntax error, unepected '[', epecting ')'

    and there are no errors in PHP 5.4. What could be a mistake?

    I'm checking here-- http://ru.piliapp.com/php-syntax-check/

    namespace BW;
    class Vkontakte
    {
    const VERSION = '5.5';
    private $appId;
    private $secret;
    private $scope = array();
    private $redirect_uri;
    private $responceType = 'code';
    private $accessToken;
    public function __construct(array $config)
    {
    if (isset($config['access_token'])) {
    $this->setAccessToken(json_encode(['access_token' => $config['access_token']])); //ошибка тут
    }
    if (isset($config['app_id'])) {
    $this->setAppId($config['app_id']);
    }
    if (isset($config['secret'])) {
    $this->setSecret($config['secret']);
    }
    if (isset($config['scopes'])) {
    $this->setScope($config['scopes']);
    }
    if (isset($config['redirect_uri'])) {
    $this->setRedirectUri($config['redirect_uri']);
    }
    if (isset($config['response_type'])) {
    $this->setResponceType($config['response_type']);
    }
    }
    public function getUserId()
    {
    return $this->accessToken->user_id;
    }
    public function setAppId($appId)
    {
    $this->appId = $appId;
    return $this;
    }
    public function getAppId()
    {
    return $this->appId;
    }
    public function setSecret($secret)
    {
    $this->secret = $secret;
    return $this;
    }
    public function getSecret()
    {
    return $this->secret;
    }
    public function setScope(array $scope)
    {
    $this->scope = $scope;
    return $this;
    }
    public function getScope()
    {
    return $this->scope;
    }
    public function setRedirectUri($redirect_uri)
    {
    $this->redirect_uri = $redirect_uri;
    return $this;
    }
    public function getRedirectUri()
    {
    return $this->redirect_uri;
    }
    public function setResponceType($responceType)
    {
    $this->responceType = $responceType;
    return $this;
    }
    public function getResponceType()
    {
    return $this->responceType;
    }
    public function getLoginUrl()
    {
    return 'https://oauth.vk.com/authorize'
    . '?client_id=' . urlencode($this->getAppId())
    . '&scope=' . urlencode(implode(',', $this->getScope()))
    . '&redirect_uri=' . urlencode($this->getRedirectUri())
    . '&response_type=' . urlencode($this->getResponceType())
    . '&v=' . urlencode(self::VERSION);
    }
    public function isAccessTokenExpired()
    {
    return time() > $this->accessToken->created + $this->accessToken->expires_in;
    }
    public function authenticate($code = NULL)
    {
    $code = $code ? $code : $_GET['code'];
    $url = 'https://oauth.vk.com/access_token'
    . '?client_id=' . urlencode($this->getAppId())
    . '&client_secret=' . urlencode($this->getSecret())
    . '&code=' . urlencode($code)
    . '&redirect_uri=' . urlencode($this->getRedirectUri());
    $token = $this->curl($url);
    $data = json_decode($token);
    $data->created = time();
    $token = json_encode($data);
    $this->setAccessToken($token);
    return $this;
    }
    public function setAccessToken($token)
    {
    $this->accessToken = json_decode($token);
    return $this;
    }
    public function getAccessToken()
    {
    return json_encode($this->accessToken);
    }
    public function api($method, array $query = array())
    {
    $parameters = array();
    foreach ($query as $param => $value) {
    $q = $param . '=';
    if (is_array($value)) {
    $q .= urlencode(implode(',', $value));
    } else {
    $q .= urlencode($value);
    }
    $parameters[] = $q;
    }
    $q = implode('&', $parameters);
    if (count($query) > 0) {
    $q .= '&';
    }
    $url = 'https://api.vk.com/method/' . $method . '?' . $q . 'access_token=' . $this->accessToken->access_token;
    $result = json_decode($this->curl($url));
    return $result;
    }
    protected function curl($url)
    {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $result = curl_exec($ch);
    if (!$result) {
    $errno = curl_errno($ch);
    $error = curl_error($ch);
    }
    curl_close($ch);
    if (isset($errno) && isset($error)) {
    throw new \Exception($error, $errno);
    }
    return $result;
    }
    public function postToPublic($publicID, $text, $fullServerPathToImage)
    {
    $types = getimagesize($fullServerPathToImage);
    $type = $types['mime'];
    if ($type ==  'image/jpeg' || $type ==  'image/png') {
    $response = $this->api('photos.getWallUploadServer', [
    'group_id' => $publicID,
    ]);
    $uploadURL = $response->response->upload_url;
    $output = [];
    $datab = array('photo' => '@'.$fullServerPathToImage);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $uploadURL);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $datab);
    $output = curl_exec($curl);
    curl_close($curl);
    $response = json_decode($output);
    $response = $this->api('photos.saveWallPhoto', [
    'group_id' => $publicID,
    'photo' => $response->photo,
    'server' => $response->server,
    'hash' => $response->hash,
    ]);
    if ($tags) {
    $text .= "\n\n";
    }
    foreach ($tags as $tag) {
    $text .= ' #' . str_replace(' ', '_', $tag);
    }
    $text = html_entity_decode($text);
    $response = $this->api('wall.post',
    [
    'owner_id' => -$publicID,
    'from_group' => 1,
    'attachments' => "{$response->response[0]->id}",
    ]);
    return true;
    }
    if ($type ==  'image/gif') {$response = $this->api('docs.getUploadServer', [
    ]);
    $uploadURL = $response->response->upload_url;
    $output = [];
    $datab = array('file' => '@'.$fullServerPathToImage);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $uploadURL);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $datab);
    $output = curl_exec($curl);
    curl_close($curl);
    $response = json_decode($output);
    $response2 = $this->api('docs.save', [
    'file' => $response->file,
    'title' => 'site.ru',
    'tags' => 'site.ru'
    ]);
    if ($tags) {
    $text .= "\n\n";
    }
    foreach ($tags as $tag) {
    $text .= ' #' . str_replace(' ', '_', $tag);
    }
    $text = html_entity_decode($text);
    $response = $this->api('wall.post',
    [
    'owner_id' => -$publicID,
    'from_group' => 1,
    'message' => "$text",
    'attachments' => 'doc'.$response2->response[0]->owner_id.'_'.$response2->response[0]->did,
    ]);
    return true;
    }
    }
    }
    

  • QA Engineer

    Until version 5.4, PHP did not maintain a short syntax for masses.

    []; // PHP 5.4+ array
    array(); // PHP 5.3 array
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2