【问题标题】:Cannot retrieve JSON POST via PHP: cURL无法通过 PHP 检索 JSON POST:cURL
【发布时间】:2013-02-13 00:04:11
【问题描述】:

我尝试实现以下 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 吗?非常感谢任何提示、建议等。谢谢。

【问题讨论】:

  • 检查您的响应的标题,如内容类型!
  • $jsonStr 在哪里?你在哪里分配它?

标签: php json post curl force.com


【解决方案1】:

我自己解决了这个问题。我不确定我这样做是否正确,但事实证明我的代码是完美的。我不断刷新我的网站,从那里发布到一些 FORCE.COM 网站。我相信管理一些 FORCE.COM 网站的人最终遇到了问题。我所做的一切都没有错。出于某种原因,我得到了一个代码 202 和一些乱码。我很乐意显示输出,但为了管理我要发布的某些 FORCE.COM 网站的人,我不想再次发布。谢谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    相关资源
    最近更新 更多