简体   繁体   English

我可以将PHP变量插入JSON字符串吗?

[英]Can I insert PHP variables into a JSON string?

I am using a REST API to POST attributes to a person using json. 我正在使用REST API将属性POST到使用json的人。 My request body looks like this: 我的请求正文如下所示:

$requestBody = '

{
    "attribute": {
        "@id": "",
        "@uri": "",
        "person": {
            "@id": "222",
            "@uri": "https://api_name_removed.com/v1/People/222"
        },
        "attributeGroup": {
            "@id": "",
            "@uri": "",
            "name": null,
            "attribute": {
                "@id": "2404",
                "@uri": "",
                "name": null
            }
        },
        "lastUpdatedDate": null
    }
}';

How do I change the person id, person uri and attribute id to be variables I have already stored? 如何将人员ID,人员uri和属性ID更改为已经存储的变量?

$requestBody = '

{
    "attribute": {
        "@id": "' . $id . '",
        "@uri": "' . $uri . '",
        "person": {
            "@id": "222",
            "@uri": "https://api_name_removed.com/v1/People/222"
        },
        "attributeGroup": {
            "@id": "",
            "@uri": "",
            "name": null,
            "attribute": {
                "@id": "2404",
                "@uri": "",
                "name": null
            }
        },
        "lastUpdatedDate": null
    }
}';
$requestBody = sprintf('
{
    "attribute": {
        "@id": "%u",
        "@uri": "%s",
        "person": {
            "@id": "222",
            "@uri": "https://api_name_removed.com/v1/People/222"
        },
        "attributeGroup": {
            "@id": "",
            "@uri": "",
            "name": null,
            "attribute": {
                "@id": "2404",
                "@uri": "",
                "name": null
            }
        },
        "lastUpdatedDate": null
    }
}', $id, $uri);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM