Skip to content

Instantly share code, notes, and snippets.

@ryrun
Created February 12, 2020 08:29
Show Gist options
  • Save ryrun/8a61ecd1a20265be655bbcaa0e5363e8 to your computer and use it in GitHub Desktop.
Save ryrun/8a61ecd1a20265be655bbcaa0e5363e8 to your computer and use it in GitHub Desktop.
Using OpenProject API in PHP, some exmaples
<?php
/**
The current openproject api is missing some exmaples. This is what i got working in PHP.
*/
function _request( $req, $postdata = false, $requesttype = 'POST' )
{
$ch = curl_init();
if ( preg_match( '~^/api/v3(.*)~', $req, $temp ) ) {
$req = $temp[ 1 ];
}
//change url here
curl_setopt( $ch, CURLOPT_URL, 'http://***/api/v3' . $req );
//change api key here
curl_setopt( $ch, CURLOPT_USERPWD, "apikey:xxx" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
if ( $postdata ) {
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $requesttype );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen( $postdata ) )
);
}
$return = curl_exec( $ch );
curl_close( $ch );
return $return;
}
//list projects
$projects = json_decode( _request( '/projects' ) );
//get project data
$projectdata = json_decode( _request( '/projects/' . $project->id ) );
//get work packages
$pagesize = 20;
$offset = 1;
$workpackages = json_decode( _request( '/projects/' . $project->id . '/work_packages?offset=' . $offset . '&pageSize=' . $pagesize ) );
//remove project status
_request( '/api/v3/projects/' . $projectid, '{"status": null}', 'PATCH' );
//set project status: on track, at risk, off track
_request( '/api/v3/projects/' . $projectid, '{"status": "on track"}', 'PATCH' );
//set project status text
_request( '/api/v3/projects/' . $projectid, '{"statusExplanation": { "format": "markdown", "raw": "my sattus text" }}', 'PATCH' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment