在curl中发布带有标题的数据在PHP中不起作用? [英] Post the data with header in curl not working in php?

查看:81
本文介绍了在curl中发布带有标题的数据在PHP中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP创建Box应用程序用户.用于创建用户的curl如下,并且正在终端上工作

I'm trying to create the box app users using PHP. The curl for create user as follows, and it is working on terminal

curl https://api.box.com/2.0/users \
-H "Authorization: Bearer <Access token>" \
-d '{"name": "New User", "is_platform_access_only": true}' \
-X POST

我用php尝试过的东西,但是它给出了以下错误

Same thing I have tried with php But it is giving the following error

{"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"6688622675982fb5339a37"}

以下我尝试过的

$developer_token = "TOKEN" ;
$access_token_url = "https://api.box.com/2.0/users";

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $access_token_url);

//Adding Parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'name'=>'NEW USER', 
    'is_platform_access_only'=>'true',
     ));

//Adding Header 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer '.$developer_token
     ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response1 = curl_exec($ch);

如果我删除Post参数,并且仅使用标头运行,则会给出用户结果.但是在发布时会引发错误.

推荐答案

我在

I have rise the same question in Perl tag with Perl code. There I got answer by user @melpomene.

我们应该将数据编码为JSON.这是工作, 那么最后的代码是

We should encode the data as JSON. It is working, Then the final code is

$data = array(name=>SOMENAME,is_platform_access_only=>true);
$data = json_encode($data);
$header = array("Authorization: Bearer <TOKEN>");
$ch = curl_init("https://api.box.com/2.0/users/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
curl_close($ch);

这篇关于在curl中发布带有标题的数据在PHP中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆