我尝试实现以下 PHP 代码以通过 PHP 发布 JSON:cURL(SOME FORCE.COM WEBSITE 是一个标记,表示我要发布的 URL):
$url = "<SOME FORCE.COM WEBSITE>";
$data =
'application' =>
array
(
'isTest' => FALSE,
key => value,
key => value,
key => value,
...
)
$ch = curl_init($url);
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POST, true);
//Send blindly the json-encoded string.
//The server, IMO, expects the body of the HTTP request to be in JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array
(
'Content-Type:application/json',
'Content-Length: ' . strlen($data_string)
)
);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
echo '<pre>';
echo $_POST;
$jsonStr = file_get_contents('php://input'); //read the HTTP body.
var_dump($jsonStr);
var_dump(json_decode($jsonStr));
echo '</pre>';
上面的输出如下:
"Your TEST POST is correct, please set the isTest (Boolean) attribute on the application to FALSE to actually apply."
Arraystring(0) ""
NULL
好的,以上确认我使用 json_encode 正确格式化了 JSON 数据,并且 SOME FORCE.COM 网站承认 'isTest' 的值为 FALSE。但是,我没有从“var_dump($jsonStr)”或“var_dump(json_decode($jsonStr))”中得到任何东西。我决定忽略这个事实并将'isTest'设置为FALSE,假设我没有得到任何JSON数据,因为我将'isTest'设置为TRUE,但是当我将'isTest'设置为FALSE时会出现混乱:
[{"message":"System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing body, need at least one of html or plainText: []\n\nClass.careers_RestWebService.sendReceiptEmail: line 165, column 1\nClass.careers_RestWebService.postApplication: line 205, column 1","errorCode":"APEX_ERROR"}]
Arraystring(0) ""
NULL
我仍然没有收到任何 JSON 数据,最终无法发送电子邮件。我相信这个问题是由空的电子邮件正文引起的,因为“var_dump($jsonStr)”或“var_dump(json_decode($jsonStr))”没有任何内容。你能帮我检索 JSON POST 吗?我真的很感激任何提示、建议等。谢谢。