在字段值之一中使用ENTER接收PHP中的JSON POST

时间:2019-06-18 20:01:51

标签: php json

当“描述”字段具有“输入”(换行符)时,API失败。

图片以检查用户发送的所有参数

a busy cat

下面的代码从发布的JSON获取数据。

     // get posted data
     $jason_value = json_decode(file_get_contents("php://input"));
     $crm_id = $jason_value->data->crmId;
     $descriptions = $jason_value->data->descriptions;

我希望将描述作为一个字符串接受。

descriptions =“ 10个以上的Windows现代风格7057655959”。

我无权访问用户在其中输入说明的程序,我可以在其中添加验证并将其转换为\ n。

转换后在字符串下方获取

{ "jwt": "eyJ0", "data": { "crmId": "15876047", "geoconceptAppointmentId": "15876","geoconceptCustomerId": "15876047","status": "Rejected","appointmentDateTime": "","firstName": "Nick Test","lastName": "PA","address": "9112 RUE Tom","city": "MONTREAL","state": "QC","zip": "H2N1T1","country": "CAN","phoneNumber1": "5148332222","phoneNumber2": "5148332222","email": "nbskgg@gmail.com","dateEntry": "2019-06-20 12:02","dateModify": "2019-06-20 12:02","preferredWayToContact": "","textMsgFlag": "Y","hearAboutUs": "Referral","perferredTime": "Anytime","descriptions": "I have to call at 5" pm. ","worklog": "This is the comment ","rejectReason": "Area | Region","referredByDC": "09999","referredByStoreUsername": "store215","assignedUsername": "","createdByUsername": "np","modifiedByUsername": "np","btgMarket": "Montreal"}}

1 个答案:

答案 0 :(得分:1)

您是正确的,在PHP7 +中,文字选项卡或换行符将导致json解析失败。 file_get_contents("php://input")返回一个字符串,所以我看不出为什么在尝试解析它之前不能仅对其进行过滤。但是也许我想念一些东西。

//Catch Unix OR DOS line endings, but not both
$filter = Array("\n","\n\r");
$replace = " ";
$cleanJSON = str_replace($filter, $replace, file_get_contents("php://input");
$data = json_decode($cleanJSON));

我想指出,在此之后,您的代码引用了一个不存在的变量:$jason_value

$crm_id = $jason_value->data->crmId;
$descriptions = $jason_value->data->descriptions;

要引用刚刚创建的对象的属性,请直接转到$data

$crm_id = $data->crmId;
$descriptions = $data->descriptions;

我希望您想用一个空格替换换行符,但是如果您实际遇到的内容在换行符之前有一个空格,那么您可能只想要一个空字符串,但这无法从我们所拥有的内容中分辨出来。