views:

44

answers:

2

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?

+1  A: 
$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
    }
}';
Devin Ceartas
You are AWESOME! Thank you. I am new to this and wasn't sure of the syntax but this works beautifully. Thanks again! :)
motboys
A: 
$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);
devzorg