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

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

用于检查用户发送的所有参数的图像

http://img2.mukewang.com/61299d40000148e406350664.jpg

下面的代码从发布的 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"}}



胡子哥哥
浏览 174回答 1
1回答

HUWWW

你是对的,在 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;我希望您想用空格替换换行符,但如果您实际遇到的内容在换行符之前有一个空格,则您可能只想要一个空字符串,但无法从我们拥有的内容中看出这一点。
打开App,查看更多内容
随时随地看视频慕课网APP