我可以将PHP变量插入JSON字符串吗?
问题描述:
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?
我正在使用REST API将属性POST给使用json的人。 我的请求正文如下所示: p>
$ requestBody ='
{
“属性”:{
“@id”:“”,
“ @uri“:”“,
”人“:{
”@id“:”222“,
”@ uri“:”https://api_name_removed.com/v1/People/222"
} ,
“attributeGroup”:{
“@id”:“”,
“@ uy”:“”,
“name”:null,
“属性”:{
“@id”: “2404”,
“@ uy”:“”,
“名称”:null
}
},
“lastUpdatedDate”:null
}
}';
code>
如何将人员ID,人员uri和属性ID更改为我已存储的变量? p>
div>
答
$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);