gpt4 book ai didi

php - 使用 PHP 发布嵌套 JSON 时遇到问题

转载 作者:搜寻专家 更新时间:2023-10-31 21:12:58 25 4
gpt4 key购买 nike

我正在尝试将一些表单数据作为 JSON 对象发送到 sample app on Force.com 。我使用 jQuery 获取表单数据并将其发布到我服务器上的 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;

?>

将表单数据发布到 PHP 文件的客户端 jQuery 代码:

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"}

有人知道如何解决这个问题吗?

最佳答案

从那些 var_dump 的外观来看,我认为您需要在使用 $data 之前从 $data 中去除这些斜杠。尝试 stripslashes .

关于php - 使用 PHP 发布嵌套 JSON 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15165530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com