2010-02-13 112 views
0

我正在使用REST API将属性发布给使用json的人员。我的请求正文如下所示:我可以将PHP变量插入JSON字符串吗?

$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 
    } 
}'; 

如何将人员ID,人员URI和属性ID更改为我已存储的变量?

回答

4
$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 
    } 
}'; 
+0

你是真棒!谢谢。我对此很陌生,不确定的语法,但这很好用。再次感谢! :) – pgtips 2010-02-13 05:06:44

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