使用PHP发送嵌套JSON时出现问题(POST请求方式)

3
我试图将一些表单数据作为JSON对象发送到 Force.com上的示例应用程序。我使用jQuery获取表单数据,并将其POST到我的服务器上的PHP文件,然后再将其发送到上面链接的示例应用程序。但是,我从示例应用程序获得的响应告诉我,在某些方面我犯了一些错误。 与示例Force.com应用程序通信的PHP文件:
<?php

    $url = 'https://cmsamp.secure.force.com/GenericApp/services/apexrest/GenericApp';
    $data = $_POST[ 'data'];

    $options = array('http' => array('method'  => 'POST','content' => http_build_query($data)));
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    echo $result;

?>

客户端jQuery代码,将表单数据提交到PHP文件:
var sample_form_data = {"attributes":{"type":"Generic_App__c"},"Application_Type__c":"iPay","Return_Email__c":"lsmith@cmsrep.com","Name":"Bus Test","ACHRejectFee__c":"123456789","ApplicationDate__c":"2000-01-01","BusinessPhone__c":"(555) 123-4567","Email__c":"thetest@testemail.com","InternetPercentage2__c":"0","MailingState__c":"CA","MOTO7__c":"true","NumOfLocations__c":"15"};

$.post( url, { data: JSON.stringify( sample_form_data ) }, function( result ) { 
    console.log( result );
});

我从Force.com应用程序中得到的响应是:
"No content to map to Object due to end of inputInsert failed. 
First exception on row 0; 
first error: REQUIRED_FIELD_MISSING, 
Required fields are missing: [Name]: [Name]"

期望的“成功”响应:

"Success: 
Generic App Object: Bus Test; was successfully created and inserted"

这是 PHP 代码中 var_dump($data) 的输出结果(为了易读性添加了换行符):

string(405)
    "{\"attributes\":
        {\"type\":\"Generic_App__c\"},
    \"Application_Type__c\":\"iPay\",
    \"‌​Return_Email__c\":\"lsmith@cmsrep.com\",
    \"Name\":\"Bus Test\",
    \"ACHRejectFee__c\":\"123456789\",
    \"ApplicationDate__c\":\"2000-01-01\",
    \"B‌​usinessPhone__c\":\"(555) 123-4567\",
    \"Email__c\":\"thetest@testemail.com\",
    \"InternetPercentage2__c\":\"0\"‌​,
    \"MailingState__c\":\"CA\",
    \"MOTO7__c\":\"true\",
    \"NumOfLocations__c\":\"15\"
    }" 

通用应用程序只需获得一个带有正确字段的JSON对象即可。当我通过REST客户端提交以下内容时,它按预期工作(为了易读性添加了换行符):
{"attributes":
    {"type":"Generic_App__c"},
"Application_Type__c":"iPay",
"Return_Email‌​__c":"test@example.org",
"Name":"Bus Test",
"ACHRejectFee__c":"123456789",
"ApplicationDate__c":"2000-01-01",
"BusinessPho‌​ne__c":"(555) 123-4567",
"Email__c":"thetest@testemail.com",
"InternetPercentage2__c":"0",
"Mailing‌​State__c":"CA",
"MOTO7__c":"true",
"NumOfLocations__c":"15"} 

有人对如何解决这个问题有什么想法吗?


打印输出你要发送的 JSON 对象,使用 var_dump 函数。 - Sturm
同意,这个(从你的php文件中)的输出将非常有帮助 var_dump($data);。此外,是否有关于示例应用程序期望的文档?实际处理程序的响应消息不足以确定它期望的格式。 - Patrick M
@NathanMann - 这是输出结果 - string(405) "{"attributes":{"type":"Generic_App__c"},"Application_Type__c":"iPay","Return_Email__c":"lsmith@cmsrep.com","Name":"Bus Test","ACHRejectFee__c":"123456789","ApplicationDate__c":"2000-01-01","BusinessPhone__c":"(555) 123-4567","Email__c":"thetest@testemail.com","InternetPercentage2__c":"0","MailingState__c":"CA","MOTO7__c":"true","NumOfLocations__c":"15"}" - Lindsey Wilson
@PatrickM - 它只期望获得一个带有正确字段的JSON对象。当我通过REST客户端提交以下内容时,它按预期工作 - {"attributes":{"type":"Generic_App__c"},"Application_Type__c":"iPay","Return_Email__c":"test@example.org","Name":"Bus Test","ACHRejectFee__c":"123456789","ApplicationDate__c":"2000-01-01","BusinessPhone__c":"(555) 123-4567","Email__c":"thetest@testemail.com","InternetPercentage2__c":"0","MailingState__c":"CA","MOTO7__c":"true","NumOfLocations__c":"15"} - Lindsey Wilson
2个回答

2
从这些var_dumps的样子来看,我觉得在您使用$data之前需要剥离掉其中的反斜杠。可以尝试使用 stripslashes 函数。

我认为var_dump可能会自动添加这些引号,因为源字符串中有引号。不过尝试使用stripslashes也是值得一试的。 - Patrick M
添加了 stripslashes - 相同的错误 - var_dump 现在显示 - string(355) "{"attributes":{"type":"Generic_App__c"},"Application_Type__c":"iPay","Return_Email__c":"lsmith@cmsrep.com","Name":"Bus Test","ACHRejectFee__c":"123456789","ApplicationDate__c":"2000-01-01","BusinessPhone__c":"(555) 123-4567","Email__c":"thetest@testemail.com","InternetPercentage2__c":"0","MailingState__c":"CA","MOTO7__c":"true","NumOfLocations__c":"15"}" - Lindsey Wilson
好消息是,那几乎可以肯定是朝着正确方向迈出的一步。不过,你知道的坏消息是,这还不是完整的解决方案。 - user1618143

0

http_build_query 也需要一个数组。由于您的数据已经是一个json字符串,我认为您应该省略它。您可能需要对其进行url编码,但您应该只使用常规的urlencode函数。

此外,我不熟悉您正在使用的php上下文机制,而且我对php.net文档深感不满意,但我无法摆脱这种困扰的感觉,即'content' => $data正在做更多的事情,而不仅仅是设置内容。您有关于流上下文实际使用'content'选项的良好文档链接吗?


我在尝试了 stripslashes 之后尝试了这个方法 - 出现了相同的错误 - 可能是我没有完全理解应该替换什么。我将 $options = array('http' => array('method' => 'POST','content' => http_build_query($data))); 更改为 $options = array('http' => array('method' => 'POST','content' => urlencode($data))); - Lindsey Wilson
很抱歉,我不理解你在帖子的第二部分所询问的内容。我在这方面还是个新手。 - Lindsey Wilson
我猜我想问的是你在哪里或者怎么学会使用file_get_contentsstream_context_create。我也是一个初学者,所以你不用担心。 - Patrick M
这是我在StackOverflow上找到的一段代码示例。基本上,我想要做的就是将一个HTML表单用jQuery打包成JSON格式,并使用PHP将其发布到URL上。我已经有一段时间没有使用PHP了,所以我在寻找PHP脚本的起点。如果有更好的编写PHP的方法,我很乐意尝试。谢谢! - Lindsey Wilson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接