根据以下情况,我试图使用PHP使用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等效值是什么?
发布于 2015-05-26 12:20:43
试试这个:
$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命令。告诉我是怎么回事。
发布于 2015-05-26 12:23:33
比较一下CLI示例和PHP:
CLI
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
// ...
$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.'}}]',
);
//...https://stackoverflow.com/questions/30457665
复制相似问题