为什么这段 PHP 代码在一台机器上运行而在另一台机器上失败? [英] Why does this PHP code run on one machine and fail on another?

查看:56
本文介绍了为什么这段 PHP 代码在一台机器上运行而在另一台机器上失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .php 文件的开头有这个代码:

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()); 

当我在远程 Ubuntu 12.04 服务器上运行它时,它运行良好.但是当我在本地服务器(再次是 Ubuntu 12.04)上运行它时,它失败并出现以下错误:

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

我已经确保机器上安装了 CURL.我可以提供任何需要的额外信息,请问!

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?

推荐答案

不同版本的 PHP 是您的问题.它的工作原理是运行 PHP 5.4 或更高版本.另一个运行 5.3 或更高版本.实例化时的类成员访问 ((new Object)->method()) 直到 PHP 5.4 才可用.

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.

这篇关于为什么这段 PHP 代码在一台机器上运行而在另一台机器上失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆