Undo
Undo

Reputation: 25697

Why does this PHP code run on one machine and fail on another?

I have this code at the start of a .php file:

<?php

$url = 'https://api.stackexchange.com/2.1/info';
$data = array("site" => 'space', "key" => "T1h2I3s4I5s6M7y8A9p0I1k2E3y!");

$response = (new Curl)->exec($url . '?' . http_build_query($data), [CURLOPT_ENCODING => 'gzip']); 
// ^^ Line 6 ^^

$obj = json_decode($response);
$timestamp = date("Y:m:d H:i:s");

$dbhost = "localhost";
$dbname = "statengine";
$dbuser = "statengine";
$dbpass = "#P1aSs3WoR5d!";

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error()); 

When I run it on my remote Ubuntu 12.04 server, it runs fine. But when I run it on a local server (Ubuntu 12.04 again), it fails with this error:

$ php getstats.php 
PHP Parse error:  syntax error, unexpected T_OBJECT_OPERATOR in /home/<me!>/Desktop/statengine/getstats.php on line 6

I've made sure that CURL is installed on the machine. I can provide any extra info needed, just ask!

How do I make it work?

Upvotes: 1

Views: 154

Answers (1)

John Conde
John Conde

Reputation: 219924

Different versions of PHP is your issue. The one where it works is running PHP 5.4 or newer. The other one is running 5.3 or older. Class member access on instantiation ((new Object)->method()) wasn't available until PHP 5.4.

Upvotes: 4

Related Questions