You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP API无法读取命令行CURL POST的JSON参数问题求助

问题解决:命令行Curl无法传递JSON参数到PHP API

你的命令行Curl调用失败的核心原因是Shell对双引号的解析冲突,导致实际发送的JSON内容不完整或格式错误,使得API端的json_decode返回null,进而触发Trying to access array offset on null警告。

问题根源

你当前的命令行语句中,JSON字符串外部用双引号包裹,内部也使用了双引号:

curl --header "Content-Type: application/json" --request POST --data "{"orderid":123456}" https://Mydomain/MyAPIEndPoint.php

Shell会将内部的{"orderid":123456}拆分为多个片段(把{和}当成分隔符),实际发送给API的内容并不是完整的JSON结构,自然无法被正确解析。

两种修正方案

方案1:用单引号包裹JSON字符串

将JSON部分改用单引号包裹,避免Shell解析内部的双引号:

curl --header "Content-Type: application/json" --request POST --data '{"orderid":123456}' https://Mydomain/MyAPIEndPoint.php

方案2:转义内部双引号

如果必须使用双引号包裹整体,需要对JSON内部的双引号做Shell转义(添加\):

curl --header "Content-Type: application/json" --request POST --data "{\"orderid\":123456}" https://Mydomain/MyAPIEndPoint.php

验证效果

执行修正后的命令后,API端的file_get_contents("php://input")就能读取到完整的JSON字符串,json_decode可以正常解析出数组,从而正确获取orderid的值。

内容的提问来源于stack exchange,提问作者Spencer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 15:03:23