PHP 中的连接 URL

Man*_*ena 0 php url concatenation file-get-contents

当我尝试将变量与 URL 连接时遇到问题

这有效:

$id = 123;
$url = file_get_contents("http://127.0.0.1:8080/api/table.json?output=html&udptype=trap&udpmsgid=".$id."&content=udpmessage");
Run Code Online (Sandbox Code Playgroud)

但这不起作用

$id = $_GET['id'];
$url = file_get_contents("http://127.0.0.1:8080/api/table.json?output=html&udptype=trap&udpmsgid=".$id."&content=udpmessage");
Run Code Online (Sandbox Code Playgroud)

我不知道我是否正确连接了变量。

小智 5

尝试这个,

if(!empty($_GET['id']))
{
   $id = $_GET['id'];
   $url = file_get_contents("http://127.0.0.1:8080/api/table.json?output=html&udptype=trap&udpmsgid=".$id."&content=udpmessage");
}
Run Code Online (Sandbox Code Playgroud)