要在 CentOS 上使用 PHP 远程连接,您可以使用 cURL、sockets 或其他支持远程连接的库。以下是使用 cURL 的示例:
sudo yum install php-curl
sudo systemctl restart httpd
<?php
$url = "http://example.com"; // 替换为您要连接的远程 URL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
echo $output;
}
curl_close($ch);
?>
将此文件上传到您的 CentOS 服务器上的 Web 目录(例如:/var/www/html)。
使用浏览器或其他 HTTP 客户端访问此 PHP 文件(例如:http://your_server_ip/remote_connection.php)。您应该看到远程 URL 的输出。
注意:根据您的需求,您可能需要配置防火墙以允许外部访问您的 CentOS 服务器。此外,确保您有权访问要连接的远程服务器,并遵循其 API 或服务条款。