PHP API Create Request

152 views
Skip to first unread message

stefan...@sessioncam.com

unread,
Sep 6, 2016, 10:11:07 AM9/6/16
to DevTargetProcess
I'm trying to create an API call to create a new request with various bits of form data. However I am getting a little stuck with getting started. Would anyone be able to help with a bit more of a specific example using PHP to create a new Request

tsar...@targetprocess.com

unread,
Sep 6, 2016, 1:28:43 PM9/6/16
to DevTargetProcess
Hello!

First of all, we recently enforce TLS1.2 encryption: https://www.targetprocess.com/blog/2016/03/targetprocess-will-start-enforcing-tls-1-2-encryption/  

So you should choose a PHP version with a openssl library v1.0.1 and up, as support of TLS1.2 was added from openssl  1.0.1: https://web.archive.org/web/20141205180836/http://www.openssl.org/news/openssl-1.0.1-notes.html

Basic samples of CRUD operations on TargetProcess (TP) entities you can find here:
So, what do we need to use TP API?
  • TP account :)
  • Access token for authentication & authorization in TP. You can acquire access token on your profile page, 'Access Tokens' tab.
  • Simple HTTP REST client, for example, httpful: http://phphttpclient.com/
Request creation example:

require "util/httpful.phar";

$url
= "https://md5.tpondemand.com/api/v1/requests";   // Put your account API query url here.
$access_token
= "MTpjR*********==";   // Access token, see My profile / Access Tokens.

$create_response
= \Httpful\Request::post($url . "?access_token=$access_token")
 
->body('{' .
           
'name: "New request",' .   // Request name
           
'project: {' .
             
'id: "1584"' .          // Request project id
           
'}' .
       
'}')
   
->sendsJson()
   
->send();

echo
"<pre>";
print_r
($create_response);

Also, if you want to see entity metadata (what fields are present, required, etc.),
use https://md5.tpondemand.com/api/v1/Requests/meta or your TP uri with same path (api/v1/Requests/meta).

Hope, this helps!

Stefan Wright

unread,
Sep 7, 2016, 4:47:54 AM9/7/16
to devtarge...@googlegroups.com
Thanks, i got this working! there are a couple of niggles that you might be able to help with. This is my code:
//Post to TP
$url = "https://sessioncam.tpondemand.com/api/v1/requests";   // Put your account API query url here.
$access_token = "MTE5*********************************==";   // Access token, see My profile / Access Tokens.
$create_response = \Httpful\Request::post($url . "?access_token=$access_token")
->body('{' .
'name: "'.$subject.'",' .   // Request name
'description: "'.$message.'",' .   // Request description
'project: {' .
'id: "11900"' .          // Request project id
'}' .
        '}')
->sendsJson()
->send();
echo "<pre>";
print_r($create_response);

What I am wondering is how can I go about returning the Request ID that I just created? I can see its output in the $create_response object but it would be great to get this back in my response.

The other is that when raising a request via email we get an autoresponse back, I guess this is something I need to talk to our admin about setting up an alert for if its raised by the API?

Regards 

Stefan Wright

Senior Customer Success Manager



UK: +44 (0)1603 618382 | US Toll Free: 1 888-528-0896 | US Local: 1 619-382-2131

stefan...@sessioncam.com
www.sessioncam.com

 SessionCam Twitter SessionCam LinkedIn


--
You received this message because you are subscribed to a topic in the Google Groups "DevTargetProcess" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/devtargetprocess/hgXcN5T_IpA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to devtargetprocess+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tsar...@targetprocess.com

unread,
Sep 7, 2016, 7:05:15 AM9/7/16
to DevTargetProcess
What I am wondering is how can I go about returning the Request ID that I just created? I can see its output in the $create_response object but it would be great to get this back in my response.

You can use SimpleXML module integrated in PHP from version 5 to parse Request creation response.

require "util/httpful.phar";

$url
= "https://md5.tpondemand.com/api/v1/requests";   // Put your account API query url here.
$access_token
= "MTpjR*********==";   // Access token, see My profile / Access Tokens.


$raw_response
= \Httpful\Request::post($url . "?access_token=$access_token")
 
->body('{' .
           
'name: "New request 3",' .
   
'description: "Some description",' .  
   
'project: {' .
     
'id: "1584"' .

   
'}' .
       
'}')
 
->sendsJson()
 
->send();

echo
"<pre>";


$is_created
= $raw_response->code >= 200 && $raw_response->code < 300;

if ($is_created) {
   
// Use SimpleXML module (PHP 5, PHP 7) to parse result XML.
   
// Can't find a way to force httpful to accept JSON as result, headers are ignored. :(  
   $request_xml = simplexml_load_string($raw_response->body);

   
if ($request_xml) {

     
// Request created, use it.
      echo
'Created request with id ' . $request_xml['Id'];

   
} else {  

     
// Sanity check. TP has problems if we are here.  
      echo
"Problems with parsing request creation response: ";

     
foreach(libxml_get_errors() as $error) {
        echo
"<br>", $error->message;
     
}

   
}  
} else {  

   
// Process request creation error.
   echo
'Problems with request creation. HTTP code: ' . $raw_response->code .
   
'. Response body: ' . $raw_response->body;

}

The other is that when raising a request via email we get an autoresponse back, I guess this is something I need to talk to our admin about setting up an alert for if its raised by the API?
Can you, please, clarify a bit?
Looks like you want to get automatic response back to some email when request is created.

As i know, there is no way to do it automatically through API (how do we know about email we should send response back?),
but you already can send autoresponse from your code (assuming you have requester email).
To unsubscribe from this group and all its topics, send an email to devtargetproce...@googlegroups.com.

Stefan Wright

unread,
Sep 7, 2016, 9:54:37 AM9/7/16
to devtarge...@googlegroups.com
Thats awesome! so glad to have posted here and got such amazing help! I am now able to create a request via the API, and I can pull back the ID for that request also.

My other query is something we are going to look at internally, when we add a new entity we send an automatic email out to the owner and other users. We may get by without this anyway

Thank you for all your help with this

Regards 

Stefan Wright

Senior Customer Success Manager



UK: +44 (0)1603 618382 | US Toll Free: 1 888-528-0896 | US Local: 1 619-382-2131

stefan...@sessioncam.com
www.sessioncam.com

 SessionCam Twitter SessionCam LinkedIn


To unsubscribe from this group and all its topics, send an email to devtargetprocess+unsubscribe@googlegroups.com.

Aliaksei Parasiatsyeu

unread,
Dec 22, 2016, 10:45:25 AM12/22/16
to DevTargetProcess
Stefan, hi!

Just noticed that one of your questions might be left unresolved.

For "New Request created" event you configure automatic email notifications within Targetprocess UI -> Settings -> Processes -> Workflows -> Request

stefan...@sessioncam.com

unread,
Feb 24, 2017, 10:06:35 AM2/24/17
to DevTargetProcess
Hey, I know this is an old post now but I am revisiting my code to make an amendment and cannot figure out how I can do the following. 

My dev team have asked us to include a Severity level, can anyone advise me how to add that. I have tried the following:

->body('{' .
        'name: "'.$data['tpsubject'].'",' .   // Request name
        'description: "'.$data['tpmessage'].'",' .   // Request description
        'severity: "normal",' .   // Request description
        'project: {' .
        'id: "33086"' .          // Request project id
        '}' .
        '}')
        ->sendsJson()
        ->send();

and also

->body('{' .
        'name: "'.$data['tpsubject'].'",' .   // Request name
        'description: "'.$data['tpmessage'].'",' .   // Request description
        'severity: "1",' .   // Request description
        'project: {' .
        'id: "33086"' .          // Request project id
        '}' .
        '}')
        ->sendsJson()
        ->send();

and also
->body('{' .
        'name: "'.$data['tpsubject'].'",' .   // Request name
        'description: "'.$data['tpmessage'].'",' .   // Request description
        'severity: 1,' .   // Request description
        'project: {' .
        'id: "33086"' .          // Request project id
        '}' .
        '}')
        ->sendsJson()
        ->send();

But all these cause an error to be returned. I couldn't find any information online :(

tsar...@targetprocess.com

unread,
Feb 24, 2017, 1:27:10 PM2/24/17
to DevTargetProcess
Hello, Stephan.

Looks like the field you are looking for is Priority [Business value on UI] 
(tpurl/api/v1/Requests/meta => Resource references => Priority).

You can easily get all priorities available for Request entity type:

tpurl/api/v1/Priorities?where=(entityType.name%20eq%20%27Request%27)

Then just add the needed priority (by id) to the request:

require "util/httpful.phar";

$url
= "https://md5.tpondemand.com/api/v1/requests";   // Put your account API query url here.
$access_token
= "MTpjR*********==";   // Access token, see My profile / Access Tokens.


$create_response
= \Httpful\Request::post($url . "?access_token=$access_token")   // Add &format=json if you need json response.
 
->body('{' .
           
'name: "New request",' .   // Request name
           
'project: {' .
             
'id: 1584' .            // Request project id
           
'},' .
           
'priority: {' .            // The priority of the request.
             
'id: 21' .              // 'Normal' priority.
           
'}' .

       
'}')
   
->sendsJson()
   
->send();


echo
"<pre>";
print_r
($create_response);

Stefan Wright

unread,
Feb 27, 2017, 3:29:20 AM2/27/17
to devtarge...@googlegroups.com
Hey,

I tried the below code:
$create_response = \Httpful\Request::post($url . "?access_token=$access_token")
        ->body('{' .
        'name: "'.$data['tpsubject'].'",' .   // Request name
        'project: {id: "33086"}'.          // Request project id
        'priority: {id: 6}'.        
        'description: "'.$data['tpmessage'].'",' .   // Request description
        '}')
        ->sendsJson()
        ->send();

but it doesn't seem to work. I checked https://sessioncam.tpondemand.com/api/v1/Priorities?where=(entityType.name%20eq%20%27Bug%27) and it seems to show that I can use the priorities? I did try on the below also:
        $create_response = \Httpful\Request::post($url . "?access_token=$access_token")
        ->body('{' .
        'name: "'.$data['tpsubject'].'",' .   // Request name
        'description: "'.$data['tpmessage'].'",' .   // Request description
        'priority: {id: 20}'.        
        'project: {' .
        'id: "11900"' .          // Request project id
        '}' .
        '}')
        ->sendsJson()
        ->send();

But it didnt work again. I always get the below response:

Inline images 1

I also tried adding using (for example) "6" instead of just 6 but that also didnt seem to fix the issue.

Regards 

Stefan Wright

Senior Customer Success Manager

--
You received this message because you are subscribed to a topic in the Google Groups "DevTargetProcess" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/devtargetprocess/hgXcN5T_IpA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to devtargetprocess+unsubscribe@googlegroups.com.

tsar...@targetprocess.com

unread,
Feb 27, 2017, 4:16:56 AM2/27/17
to DevTargetProcess
Catched it!

There is no comma after project and description properties declarations.

In case priority with id 6 exists for bug (check via https://sessioncam.tpondemand.com/api/v1/Priorities?where=(entityType.name eq 'Bug')
you should form the following JSON request (notice comma after each but last property definition):

'{' .
   
'name: "' . $data['tpsubject'] . '",' .
   
'project: {id: 33086},' .
   
'priority: {id: 6},' .        
   
'description: "' . $data['tpmessage'] . '"' .
'}'
To unsubscribe from this group and all its topics, send an email to devtargetproce...@googlegroups.com.

Stefan Wright

unread,
Feb 27, 2017, 5:25:46 AM2/27/17
to devtarge...@googlegroups.com
Awesome, you guys rock!

Regards 

Stefan Wright

Senior Customer Success Manager


To unsubscribe from this group and all its topics, send an email to devtargetprocess+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages