Skip to content

Instantly share code, notes, and snippets.

@JonnyFunFun
Created November 25, 2013 15:14
Show Gist options
  • Save JonnyFunFun/7642759 to your computer and use it in GitHub Desktop.
Save JonnyFunFun/7642759 to your computer and use it in GitHub Desktop.
Example on how to consume Targetprocess' REST API with PHP
<?php
define('TP_URL', 'http://enzinna.tpondemand.com/');
define('TP_USER', 'admin');
define('TP_PASS', 'admin');
# GET a story
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, TP_URL."api/v1/UserStories/201?format=json");
# set our authorization header and content-type
curl_setopt($data, CURLOPT_HTTPHEADER,
array(
"Authorization: Basic " . base64_encode(TP_USER . ":" . TP_PASS),
"Content-type: application/json",
"Accept: application/json"
));
$story = json_decode(curl_exec($ch));
curl_close($ch);
# $story is now an associative array.
# CREATE a new story
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, TP_URL."api/v1/UserStories");
# set our authorization header and content-type
curl_setopt($data, CURLOPT_HTTPHEADER,
array(
"Authorization: Basic " . base64_encode(TP_USER . ":" . TP_PASS),
"Content-type: application/json",
"Accept: application/json"
));
curl_setopt($ch, CURLOPT_POST, 1);
# You can find all applicable and required fields for stories and other resources here:
# http://md5.tpondemand.com/api/v1/UserStories/meta
$story = array(
"Name" => "Story created by REST API",
"Project" => array(
"Id": 2 #we'll create the story in the project with ID = 2
),
"EntityState" => array(
"Name": "Open"
),
"Priority" => array(
"Name": "Nice to Have"
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($story));
$result = curl_exec($ch);
@richardjklein
Copy link

Having a heck of a time creating a new story. I am able to "Get a story", but only using a token that I appended to the URL. When I try to create a story I get 401 errors no matter what I try. Here is my current code. I am trying to use the same token that is working for me for retrieving a story. Any help is much appreciated.

'Story created by REST API', 'Project' => array( "Id", 226 #we'll create the story in the project with ID = 2 ), 'EntityState' => array( 'Name', 'Open' ), 'Priority' => array( 'Name', 'Nice to Have' ) ); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($story)); $result = curl_exec($ch); echo $result;

@abdul-sami
Copy link

There was an error which I have removed. Please check my https://gist.github.com/abdul-sami/11081901#file-tp-api-example-php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment