PHP 本身是一个脚本语言,主要用于 Web 开发和服务器端编程
<?php
$url = "https://api.example.com/data";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
<?php
$url = "https://api.example.com/data";
$context = stream_context_create([
'http' => [
'method' => "GET",
'header' => "User-Agent: MyCustomUserAgent\r\n"
]
]);
$result = file_get_contents($url, false, $context);
echo $result;
?>
<?php
$host = "example.com";
$port = 80;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$socket) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
exit();
}
$result = socket_connect($socket, $host, $port);
if (!$result) {
echo "socket_connect() failed.\nReason: " . socket_strerror(socket_last_error($socket)) . "\n";
exit();
}
$message = "GET / HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n";
socket_write($socket, $message, strlen($message));
while (!socket_read($socket, 1024)) {
// Empty loop to read all data
}
socket_close($socket);
?>
请注意,这些方法都需要在 PHP 环境中运行,而不是在 Linux 命令行中直接运行。但是,你可以通过在 Linux 服务器上安装 PHP 和相应的扩展,然后编写 PHP 脚本来执行网络操作。