我正在尝试使用PHP Curl根据此添加todoist API的项目:
引用此代码:
$ curl https://todoist.com/API/v6/sync -X POST \
-d token=0123456789abcdef0123456789abcdef01234567 \
-d commands='[{"type": "item_add", "temp_id": "43f7ed23-a038-46b5-b2c9-4abda9097ffa", "uuid": "997d4b43-55f1-48a9-9e66-de5785dfd69b", "args": {"content": "Task1", "project_id": 128501470}}]'
我在PHP中尝试这个:
$args = '{"content": "Task1", "project_id":'.$project_id.'}';
$url = "https://todoist.com/API/v6/sync";
$post_data = array (
"token" => $token,
"type" => "item_add",
"args" => $args,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
所以我有令牌,args,类型,但我似乎无法让它工作。
该等调用的PHP等价物是什么?
答案 0 :(得分:3)
比较CLI示例和PHP:
curl https://todoist.com/API/v6/sync -X POST \
-d token=0123456789abcdef0123456789abcdef01234567 \
-d commands='[{"type": "item_add", "temp_id": "43f7ed23-a038-46b5-b2c9-4abda9097ffa", "uuid": "997d4b43-55f1-48a9-9e66-de5785dfd69b", "args": {"content": "Task1", "project_id": 128501470}}]'
// ...
$post_data = array (
"token" => $token,
"type" => "item_add", //<-- NOT PRESENT IN CLI EXAMPLE
"args" => $args, //<-- NOT PRESENT IN CLI EXAMPLE
);
//...
CLI POST
的2个数据:-d token=...
和-d commands=...
。不过,您的PHP帖子token
,type
和args
。只需像cli请求那样发出PHP请求:
// ...
$post_data = array (
"token" => $token,
"commands" => '[{"type": "item_add", "temp_id": "43f7ed23-a038-46b5-b2c9-4abda9097ffa", "uuid": "997d4b43-55f1-48a9-9e66-de5785dfd69b", "args": {"content": "Task1", "project_id": '.$project_id.'}}]',
);
//...
答案 1 :(得分:1)
试试这个:
$url = "https://todoist.com/API/v6/sync";
$post_data = [
'token' => $token,
'commands' =>
'[{"type": "item_add", ' .
'"temp_id": "43f7ed23-a038-46b5-b2c9-4abda9097ffa", ' .
'"uuid": "997d4b43-55f1-48a9-9e66-de5785dfd69b", ' .
'"args": {"content": "Task1", "project_id":'.$project_id.'}}]'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
我还没有测试过它,但我很确定这是用PHP实现的等效curl命令。让我知道它是如何运作的。