在php中如何使用$_GET在SetBody中包含变量

hwazgwia  于 2023-03-07  发布在  PHP
关注(0)|答案(1)|浏览(149)

嗨,我怎样在PHP中使用$_GET在SetBody中包含变量.现在我有:

<?php

$name =$_GET["name"];
echo $name;
$request->setBody('{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": $nam,
        "interfaces": [
            {
                "type": 2,
                "main": 1,
                "useip": 1,
                "ip": $name,
                "dns": "",
                "port": "161",
                "details": {
                    "version": 3,
                    "bulk": 0,
                    "securityname": "mysecurityname",
                    "contextname": "",

?>

我需要从url获取名称。我正在使用infg API到zabbix与http_Request2。感谢支持。我的请求看起来像:https://localhost/zabbix. php?name = 1.1.1.1输出为:

{
    "jsonrpc":"2.0",
    "error":{
        "code":-32700,
        "message":"Parse error",
        "data":"Invalid JSON. An error occurred on the server while parsing the JSON text."
        },
    "id":null
}

我尝试使用$variablename,但不起作用

7kqas0il

7kqas0il1#

由于您使用的是单引号字符串文字,因此必须停止文字,连接值,然后重新开始文字。

$name =$_GET["name"];
echo $name;
$request->setBody('{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "' . $name . '",        // see here
        "interfaces": [
            {
                "type": 2,
                "main": 1,
                "useip": 1,
                "ip": "' . $name . '",  // and here
                "dns": "",
                "port": "161",
                "details": {
                    "version": 3,
                    "bulk": 0,
                    "securityname": "mysecurityname",
                    "contextname": "",

相关问题