将命令行curl转换为PHP curl函数

时间:2014-05-09 11:35:38

标签: php curl

我无法将此命令行curl转换为php:

结构

curl -X POST https://kanbanflow.com/api/v1/tasks -H "Content-type: application/json"
-d '{ "<PROPERTY_1>": <PROPERTY_VALUE_1>, "<PROPERTY_2>": <PROPERTY_VALUE_2>, ... "<PROPERTY_N>": <PROPERTY_VALUE_N> }'

api文档中的示例

curl -X POST https://kanbanflow.com/api/v1/tasks -H "Content-type: application/json"
-d '{ "name": "Write report", "columnId": "7ca19de0403f11e282ebef81383f3229", "color": "red" }'

我无法理解这里的 -d 是什么?以及如何以这种格式传递数据。

到目前为止,我到达这里但它没有用。

更新的代码

$data = json_encode(array('name'=>'Testing of api', 'columnId' =>"xxxxxxxxxxxxxxxxxxx",'color'=>"red"));

$token = base64_encode("apiToken:xxxxxxxxxxxxxxxxxxxxxxxxx");

$headers = array(
        "Authorization: Basic " . $token,
        "Content-type: application/json"
);

$url = "https://kanbanflow.com/api/v1/tasks";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo "<pre>";
print_r($response);

建议我在哪里错了..

更新

我想使用https://kanbanflow.com api添加任务

回复: {"errors":[{"message":"Unexpected error"}]}

2 个答案:

答案 0 :(得分:2)

我解决了我的问题,我错过了 swimlaneId 属性。我在这里发布代码以供将来参考,以便它可以帮助其他人。

现在使用如下对象

发送数据
$object = new stdClass();
   $object->name = 'Testing Task';
   $object->columnId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
   $object->swimlaneId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
   $object->color = 'green';

$url = "https://kanbanflow.com/api/v1/tasks";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($object));
$response = curl_exec($curl); 

Json_encode数组也在这里工作

$data = json_encode(array('name'=>'Testing of api', 'columnId' =>"f648831061e111e3a41aa3dbd7a40406", 'color'=>'red', 'swimlaneId' => 'd0635fc061e711e3a41aa3dbd7a40406'));

答案 1 :(得分:0)

看起来您忘了对数据进行json编码

$data = json_encode(array('name'=>'Testing of api', 'columnId' =>"xxxxxxxxxxxxxxxxxxx",'color'=>"red"));