php 将json赋值给变量,php – 使用Luracast Restler – 将POST变量作为JSON传递给正文...

为一个项目评估Lurasoft RESTler并且卡在他们的示例上 – 尝试通过post请求的主体传递JSON结构…我有一个通过URL传递数据的工作设置但是我想得到这个帖子 – 身体数据计算出:

所以,我有一个简单的方法来处理UserAccount类中定义的POST请求:

function post($_requestData = null) {

if (is_null($_requestData)) {

throw new RestException(HTTPCODE_NO_CONTENT, "requestData is null");

}

return(array('success' => array('code' => HTTPCODE_OK, 'msg' => $msg)));

}

我用curl调用:

curl -X POST http://ll2api/userprofile -H "Content-Type: application/json" -d '{"email_pro" : "mshallop@nileguide.com"}'

我每次都会回来:

{

"error": {

"code": 204,

"message": "No Content: requestData is null"

}

}

如果我使用LuraCast在其网站上建议生成REST请求的工具RESTConsole v 4.0.2,我会看到在使用请求正文中显示的“请求参数”部分定义有效负载时我的数据接收位置:

Request Url: http://ll2api/userprofile

Request Method: POST

Status Code: 200

Params: {

"email_pro": "mshallop@gmail.com"

}

最后,通过阅读他们的示例,我看到输入参数“$requestData”在其validate()方法中被分解为关联数组,将JSON输入视为关联数组…

为了完整性,这里是REQUEST标头:

Accept: application/json

Accept-Language: en

Content-Type: application/json

Connection: keep-alive

Origin: chrome-extension: //rest-console-id

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.904.0 Safari/535.7

所以,tl; dr:为了避免堵塞REST请求的URL,如何通过POST请求的主体将JSON数据传递给LuraCast RESTler并在方法中获取所述变量?

感谢任何帮助 – 这是我第一次在REST运行,所以我怀疑我的问题是pbck / nub …

谢谢!

解决方法:

我修改了你的课程,以确保它有效,并使理解变得容易

class UserAccount

{

//$request_data is a reserved name in Restler2.

//It will pass all the parameters passed as an

//associative array

function post ($request_data)

{

//$request_data will never be null

//instead it can be an empty array

if (empty($request_data)) {

throw new RestException(412, "requestData is null");

}

return array('received'=>$request_data);

}

}

首先注意参数变量的名称很重要,只有当你将其设置为$request_data时,它才会传递所有传入的参数

让我们现在尝试几个卷曲的例子

curl -d '' http://restler2.dev/test/post_example/index.php/useraccount

回报

{

"error": {

"code": 412,

"message": "Precondition Failed: requestData is empty"

}

}

接下来让我使用标准的http post vars传递一些数据

curl -d 'email=arul@luracast.com' http://restler2.dev/test/post_example/index.php/useraccount

回报

{

"received": {

"email": "arul@luracast.com"

}

}

现在让我们回到在post请求的主体中发送JSON数据

curl -X POST http://restler2.dev/test/post_example/index.php/useraccount -H "Content-Type: application/json" -d '{"email_pro" : "mshallop@nileguide.com"}'

回报

{

"received": {

"email_pro": "mshallop@nileguide.com"

}

}

希望这说清楚.

修改的例子

如果您想使用自己的变量名称,这就是它的工作方式

class UserAccount

{

//$email and $age can be null

function post ($email, $age)

{

//$request_data will never be null

//instead it can be an empty array

if (is_null($email)) {

throw new RestException(412, "email is null");

}

return array('received'=>array('email'=>$email, 'age'=>$age));

}

}

卷曲失败的结果

curl -d '' http://restler2.dev/test/post_example/index.php/useraccount

回报

{

"error": {

"code": 412,

"message": "Precondition Failed: email is null"

}

}

curl发布http vars

curl -d 'email=arul@luracast.com' http://restler2.dev/test/post_example/index.php/useraccount

回报

{

"received": {

"email": "arul@luracast.com",

"age": null

}

}

curl发布JSON

curl -X POST http://restler2.dev/test/post_example/index.php/useraccount -H "Content-Type application/json" -d '{"email" : "mshallop@nileguide.com"}'

回报

{

"received": {

"email": "mshallop@nileguide.com",

"age": null

}

}

标签:json,php,post

来源: https://codeday.me/bug/20190518/1127056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值