douju2014 2015-05-26 11:47
浏览 74
已采纳

PHP Curl / Todoist /添加项目

I am trying to add an item with the todoist API using PHP Curl according to this:

https://developer.todoist.com/?shell#add-an-item

It quotes this code:

$ 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}}]'

I am trying this in 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);

So I have the token, the args, the type but I can't seem to get it to work.

What would the PHP equivalent of that call be?

  • 写回答

2条回答 默认 最新

  • douba2705 2015-05-26 12:20
    关注

    Try this:

    $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);
    

    I haven't tested it, but I'm pretty sure this is the equivalent curl command implemented in PHP. Let me know how it works out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题