Search code examples
trelloapi

Add trello card via html form


Im very new to api stuff and want to do following: Post a new card to a trello board using a form on my page. Trello offers a api, but I dont not really know how to use it: http://mattzuba.bitbucket.org/php-trello/

My form looks like this:

<form id="trello" class="form-horizontal">
    <div class="form-group">
        <div class="col-md-6">
            <input type="text" name="Name" placeholder="Name" class="form-control" />
        </div>
        <div class="col-md-6">
            <input type="email" name="email" placeholder="eMail" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-12">
            <input type="text" name="Title" placeholder="Title" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-12">
            <textarea name="content" cols="40" rows="10" placeholder="Content" class="form-control"></textarea>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-12">
            <input type="file" name="attachment" value="1" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <a class="btn btn-primary" id="submit">Submit</a>
    </div>
</form>

Now I want to create a card that looks like this:

card-name: $title
card-content: $content
              Submitted by: $name ($email)
card-attachment: $attachment

Because this form should be public (with captcha), the oAuth is no way for me :/

I dont know how to archive this :/ If somebody gives me an example, this would be awesome :)

Can somebody help me?


Solution

  • I have put together the following code, and it works for me.

    <?php
    if ($_POST) {
      $card_name = htmlspecialchars($_POST['title']);
      $card_content = htmlspecialchars($_POST['content']) . "\n";
      $card_content .= htmlspecialchars($_POST['name']) . " (" . htmlspecialchars($_POST['email']) . ")";
    
      $trello_key          = 'YourTrelloApiKey';
      $trello_api_endpoint = 'https://api.trello.com/1';
      $trello_list_id      = 'IdOfListYouArePostingTo';
      $trello_member_token = 'AnApplicationTokenYouHaveGenerated'; // Guard this well
      
      $url = 'https://api.trello.com/1/cards';
      $fields='token='.$trello_member_token;
      $fields.='&key='.$trello_key;
      $fields.='&idList='.$trello_list_id;
      $fields.='&name='.$card_name;
      $fields.='&desc='.$card_content;
     
      $result = trello_post($url, $fields);
    }
      
    function trello_post($url, $fields){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
      curl_setopt($ch, CURLOPT_HEADER, 0);    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 0);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $output = curl_exec($ch);
      curl_close($ch);
      return json_decode($output);
    }
    ?>
    
    <form id="trello" method="post" class="form-horizontal">
        <div class="form-group">
            <div class="col-md-6">
                <input type="text" name="name" placeholder="Name" class="form-control" />
            </div>
            <div class="col-md-6">
                <input type="email" name="email" placeholder="E-Mail" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-12">
                <input type="text" name="title" placeholder="Title" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-12">
                <textarea name="content" cols="40" rows="10" placeholder="Content" class="form-control"></textarea>
            </div>
        </div>
        <div class="form-group">
            <input type="submit" value="Submit">
        </div>
    </form>

    The only aspect I haven't got working is attachment uploads. There is very little documentation to support this.