简体   繁体   中英

Can I insert PHP variables into a JSON string?

I am using a REST API to POST attributes to a person using 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?

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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