Note that there are some explanatory texts on larger screens.

plurals
  1. POError when trying to Retrieve project PHP Asana Class
    primarykey
    data
    text
    <p>Am using <a href="https://github.com/ajimix/asana-api-php-class" rel="nofollow">this class</a> to get projects and its task. Am getting the project successfully. But when trying to get the tasks its returns. </p> <pre><code>Error while trying to connect to Asana, response code: 400 </code></pre> <p>Below is the code, its exactly from the example. Project code I got when asana returned it in the other example, where i can get the project list.</p> <pre><code>&lt;?php require_once("../asana.php"); // See class comments and Asana API for full info $asana = new Asana("xxxxxxxxxxxxxxxxx"); // Your API Key, you can get it in Asana $projectId = 'xxxxxxxxxxxxx'; // Your Project ID Key, you can get it in Asana $result = $asana-&gt;getProjectTasks($projectId); // As Asana API documentation says, when response is successful, we receive a 200 in response so... if($asana-&gt;responseCode == "200" &amp;&amp; !is_null($result)){ $resultJson = json_decode($result); var_dump($resultJson); } else { echo "Error while trying to connect to Asana, response code: {$asana-&gt;responseCode}"; } </code></pre> <p>Here is the code in the class.</p> <pre><code>&lt;?php /** * * A PHP class that acts as wrapper for Asana API. Lets make things easy! :) * * Read Asana API documentation for fully use this class http://developer.asana.com/documentation/ * * Copyright 2012 Ajimix * Licensed under the Apache License 2.0 * * Author: Ajimix [github.com/ajimix] * Version: 1.0 * */ class Asana { private $timeout = 10; private $debug = false; private $advDebug = false; // Note that enabling advanced debug will include debugging information in the response possibly breaking up your code private $asanaApiVersion = "1.0"; public $responseCode; private $endPointUrl; private $apiKey; private $taskUrl; private $userUrl; private $projectsUrl; private $workspaceUrl; private $storiesUrl; private $tagsUrl; public function __construct($apiKey){ if(substr($apiKey, -1) != ":") $apiKey .= ":"; // If the API key is not ended by ":", we append it $this-&gt;apiKey = $apiKey; $this-&gt;endPointUrl = "https://app.asana.com/api/{$this-&gt;asanaApiVersion}/"; $this-&gt;taskUrl = $this-&gt;endPointUrl."tasks"; $this-&gt;userUrl = $this-&gt;endPointUrl."users"; $this-&gt;projectsUrl = $this-&gt;endPointUrl."projects"; $this-&gt;workspaceUrl = $this-&gt;endPointUrl."workspaces"; $this-&gt;storiesUrl = $this-&gt;endPointUrl."stories"; $this-&gt;tagsUrl = $this-&gt;endPointUrl."tags"; define("METHOD_POST", 1); define("METHOD_PUT", 2); define("METHOD_GET", 3); } /** * ********************************** * User functions * ********************************** */ /** * Returns the full user record for a single user. * Call it without parameters to get the users info of the owner of the API key. * * @param string $userId * @return string JSON or null */ public function getUserInfo($userId = null){ if(is_null($userId)) $userId = "me"; return $this-&gt;askAsana($this-&gt;userUrl."/{$userId}"); } /** * Returns the user records for all users in all workspaces you have access. * * @return string JSON or null */ public function getUsers(){ return $this-&gt;askAsana($this-&gt;userUrl); } /** * ********************************** * Task functions * ********************************** */ /** * Function to create a task. * For assign or remove the task to a project, use the addProjectToTask and removeProjectToTask. * * * @param array $data Array of data for the task following the Asana API documentation. * Example: * * array( * "workspace" =&gt; "1768", * "name" =&gt; "Hello World!", * "notes" =&gt; "This is a task for testing the Asana API :)", * "assignee" =&gt; "176822166183", * "followers" =&gt; array( * "37136", * "59083" * ) * ) * * @return string JSON or null */ public function createTask($data){ $data = array("data" =&gt; $data); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;taskUrl, $data, METHOD_POST); } /** * Returns task information * * @param string $taskId * @return string JSON or null */ public function getTask($taskId){ return $this-&gt;askAsana($this-&gt;taskUrl."/{$taskId}"); } /** * Updates a task * * @param string $taskId * @param array $data See, createTask function comments for proper parameter info. * @return string JSON or null */ public function updateTask($taskId, $data){ $data = array("data" =&gt; $data); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;taskUrl."/{$taskId}", $data, METHOD_PUT); } /** * Returns the projects associated to the task. * * @param string $taskId * @return string JSON or null */ public function getProjectsForTask($taskId){ return $this-&gt;askAsana($this-&gt;taskUrl."/{$taskId}/projects"); } /** * Adds a project to task. If successful, will return success and an empty data block. * * @param string $taskId * @param string $projectId * @return string JSON or null */ public function addProjectToTask($taskId, $projectId){ $data = array("data" =&gt; array("project" =&gt; $projectId)); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;taskUrl."/{$taskId}/addProject", $data, METHOD_POST); } /** * Removes project from task. If successful, will return success and an empty data block. * * @param string $taskId * @param string $projectId * @return string JSON or null */ public function removeProjectToTask($taskId, $projectId){ $data = array("data" =&gt; array("project" =&gt; $projectId)); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;taskUrl."/{$taskId}/removeProject", $data, METHOD_POST); } /** * Returns task by a given filter. * For now (limited by Asana API), you may limit your query either to a specific project or to an assignee and workspace * * NOTE: As Asana API says, if you filter by assignee, you MUST specify a workspaceId and viceversa. * * @param array $filter The filter with optional values. * * array( * "assignee" =&gt; "", * "project" =&gt; 0, * "workspace" =&gt; 0 * ) * * @return string JSON or null */ public function getTasksByFilter($filter = array("assignee" =&gt; "", "project" =&gt; "", "workspace" =&gt; "")){ $url = ""; $filter = array_merge(array("assignee" =&gt; "", "project" =&gt; "", "workspace" =&gt; ""), $filter); $url .= $filter["assignee"] != ""?"&amp;assignee={$filter["assignee"]}":""; $url .= $filter["project"] != ""?"&amp;project={$filter["project"]}":""; $url .= $filter["workspace"] != ""?"&amp;workspace={$filter["workspace"]}":""; if(strlen($url) &gt; 0) $url = "?".substr($url, 1); return $this-&gt;askAsana($this-&gt;taskUrl.$url); } /** * Returns the list of stories associated with the object. * As usual with queries, stories are returned in compact form. * However, the compact form for stories contains more information by default than just the ID. * There is presently no way to get a filtered set of stories. * * @param string $taskId * @return string JSON or null */ public function getTaskStories($taskId){ return $this-&gt;askAsana($this-&gt;taskUrl."/{$taskId}/stories"); } /** * Adds a comment to a task. * The comment will be authored by the authorized user, and timestamped when the server receives the request. * * @param string $taskId * @param string $text * @return string JSON or null */ public function commentOnTask($taskId, $text = ""){ $data = array( "data" =&gt; array( "text" =&gt; $text ) ); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;taskUrl."/{$taskId}/stories", $data, METHOD_POST); } /** * Adds a tag to a task. If successful, will return success and an empty data block. * * @param string $taskId * @param string $tagId * @return string JSON or null */ public function addTagToTask($taskId, $tagId){ $data = array("data" =&gt; array("tag" =&gt; $tagId)); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;taskUrl."/{$taskId}/addTag", $data, METHOD_POST); } /** * Removes a tag from a task. If successful, will return success and an empty data block. * * @param string $taskId * @param string $tagId * @return string JSON or null */ public function removeTagFromTask($taskId, $tagId){ $data = array("data" =&gt; array("tag" =&gt; $tagId)); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;taskUrl."/{$taskId}/removeTag", $data, METHOD_POST); } /** * ********************************** * Projects functions * ********************************** */ /** * Function to create a project. * * @param array $data Array of data for the project following the Asana API documentation. * Example: * * array( * "workspace" =&gt; "1768", * "name" =&gt; "Foo Project!", * "notes" =&gt; "This is a test project" * ) * * @return string JSON or null */ public function createProject($data){ $data = array("data" =&gt; $data); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;projectsUrl, $data, METHOD_POST); } /** * Returns the full record for a single project. * * @param string $projectId * @return string JSON or null */ public function getProject($projectId){ return $this-&gt;askAsana($this-&gt;projectsUrl."/{$projectId}"); } /** * Returns the projects in all workspaces containing archived ones or not. * * @param boolean $archived Return archived projects or not * @param string $opt_fields Return results with optional parameters */ public function getProjects($archived = false, $opt_fields = ""){ $archived = $archived?"true":"false"; $opt_fields = ($opt_fields != "")?"&amp;opt_fields={$opt_fields}":""; return $this-&gt;askAsana($this-&gt;projectsUrl."?archived={$archived}{$opt_fields}"); } /** * Returns the projects in provided workspace containing archived ones or not. * * @param string $workspaceId * @param boolean $archived Return archived projects or not */ public function getProjectsInWorkspace($workspaceId, $archived = false){ $archived = $archived?"true":"false"; return $this-&gt;askAsana($this-&gt;projectsUrl."?archived={$archived}&amp;workspace={$workspaceId}"); } /** * This method modifies the fields of a project provided in the request, then returns the full updated record. * * @param string $projectId * @param array $data An array containing fields to update, see Asana API if needed. * Example: array("name" =&gt; "Test", "notes" =&gt; "It's a test project"); * * @return string JSON or null */ public function updateProject($projectId, $data){ $data = array("data" =&gt; $data); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;projectsUrl."/{$projectId}", $data, METHOD_PUT); } /** * Returns all unarchived tasks of a given project * * @param string $projectId * * @return string JSON or null */ public function getProjectTasks($projectId){ return $this-&gt;askAsana($this-&gt;taskUrl."?project={$projectId}"); } /** * Returns the list of stories associated with the object. * As usual with queries, stories are returned in compact form. * However, the compact form for stories contains more * information by default than just the ID. * There is presently no way to get a filtered set of stories. * * @param string $projectId * @return string JSON or null */ public function getProjectStories($projectId){ return $this-&gt;askAsana($this-&gt;projectsUrl."/{$projectId}/stories"); } /** * Adds a comment to a project * The comment will be authored by the authorized user, and timestamped when the server receives the request. * * @param string $projectId * @param string $text * @return string JSON or null */ public function commentOnProject($projectId, $text = ""){ $data = array( "data" =&gt; array( "text" =&gt; $text ) ); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;projectsUrl."/{$projectId}/stories", $data, METHOD_POST); } /** * ********************************** * Tags functions * ********************************** */ /** * Returns the full record for a single tag. * * @param string $tagId * @return string JSON or null */ public function getTag($tagId){ return $this-&gt;askAsana($this-&gt;tagsUrl."/{$tagId}"); } /** * Returns the full record for all tags in all workspaces. * * @return string JSON or null */ public function getTags(){ return $this-&gt;askAsana($this-&gt;tagsUrl); } /** * Modifies the fields of a tag provided in the request, then returns the full updated record. * * @param string $tagId * @param array $data An array containing fields to update, see Asana API if needed. * Example: array("name" =&gt; "Test", "notes" =&gt; "It's a test tag"); * * @return string JSON or null */ public function updateTag($tagId, $data){ $data = array("data" =&gt; $data); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;tagsUrl."/{$tagId}", $data, METHOD_PUT); } /** * Returns the list of all tasks with this tag. Tasks can have more than one tag at a time. * * @param string $tagId * @return string JSON or null */ public function getTasksWithTag($tagId){ return $this-&gt;askAsana($this-&gt;tagsUrl."/{$tagId}/tasks"); } /** * ********************************** * Stories and comments functions * ********************************** */ /** * Returns the full record for a single story. * * @param string $storyId * @return string JSON or null */ public function getSingleStory($storyId){ return $this-&gt;askAsana($this-&gt;storiesUrl."/{$storyId}"); } /** * ********************************** * Workspaces functions * ********************************** */ /** * Returns all the workspaces. * * @return string JSON or null */ public function getWorkspaces(){ return $this-&gt;askAsana($this-&gt;workspaceUrl); } /** * Currently the only field that can be modified for a workspace is its name (as Asana API says). * This method returns the complete updated workspace record. * * @param array $data * Example: array("name" =&gt; "Test"); * * @return string JSON or null */ public function updateWorkspace($workspaceId, $data = array("name" =&gt; "")){ $data = array("data" =&gt; $data); $data = json_encode($data); return $this-&gt;askAsana($this-&gt;workspaceUrl."/{$workspaceId}", $data, METHOD_PUT); } /** * Returns tasks of all workspace assigned to someone. * Note: As Asana API says, you must specify an assignee when querying for workspace tasks. * * @param string $workspaceId The id of the workspace * @param string $assignee Can be "me" or user ID * * @return string JSON or null */ public function getWorkspaceTasks($workspaceId, $assignee = "me"){ return $this-&gt;askAsana($this-&gt;taskUrl."?workspace={$workspaceId}&amp;assignee={$assignee}"); } /** * Returns tags of all workspace. * * @param string $workspaceId The id of the workspace * * @return string JSON or null */ public function getWorkspaceTags($workspaceId){ return $this-&gt;askAsana($this-&gt;workspaceUrl."/{$workspaceId}/tags"); } /** * Returns users of all workspace. * * @param string $workspaceId The id of the workspace * * @return string JSON or null */ public function getWorkspaceUsers($workspaceId){ return $this-&gt;askAsana($this-&gt;workspaceUrl."/{$workspaceId}/users"); } /** * This function communicates with Asana REST API. * You don't need to call this function directly. It's only for inner class working. * * @param string $url * @param string $data Must be a json string * @param int $method See constants defined at the beginning of the class * @return string JSON or null */ private function askAsana($url, $data = null, $method = METHOD_GET){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Don't print the result curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this-&gt;timeout); curl_setopt($curl, CURLOPT_TIMEOUT, $this-&gt;timeout); curl_setopt($curl, CURLOPT_FAILONERROR, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // Don't verify SSL connection curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // "" "" curl_setopt($curl, CURLOPT_USERPWD, $this-&gt;apiKey); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); // Send as JSON if($this-&gt;advDebug){ curl_setopt($curl, CURLOPT_HEADER, true); // Display headers curl_setopt($curl, CURLOPT_VERBOSE, true); // Display communication with server } if($method == METHOD_POST){ curl_setopt($curl, CURLOPT_POST, true); } else if($method == METHOD_PUT){ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); } if(!is_null($data) &amp;&amp; ($method == METHOD_POST || $method == METHOD_PUT)){ curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } try { $return = curl_exec($curl); $this-&gt;responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if($this-&gt;debug || $this-&gt;advDebug){ echo "&lt;pre&gt;"; print_r(curl_getinfo($curl)); echo "&lt;/pre&gt;"; } } catch(Exception $ex){ if($this-&gt;debug || $this-&gt;advDebug){ echo "&lt;br&gt;cURL error num: ".curl_errno($curl); echo "&lt;br&gt;cURL error: ".curl_error($curl); } echo "Error on cURL"; $return = null; } curl_close($curl); return $return; } } </code></pre> <p><a href="http://developer.asana.com/documentation/" rel="nofollow">Here is Asana API doc</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload