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

佐手、 提交于 2020-02-05 09:07:18

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/20256042/why-does-this-php-code-run-on-one-machine-and-fail-on-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!