2013-03-10 3 views
0

this class을 사용하면 프로젝트 및 작업을 가져올 수 있습니다. 프로젝트를 성공적으로 수행하고 있습니다. 그러나 작업을 반환하려고 할 때 반환합니다.프로젝트 가져 오기 중 오류가 발생했습니다. PHP Asana 클래스

Error while trying to connect to Asana, response code: 400 

다음은 코드 예입니다. 프로젝트 코드 나는 다른 예제에서 asana가 반환했을 때 프로젝트 목록을 얻을 수있다.

<?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->getProjectTasks($projectId); 

// As Asana API documentation says, when response is successful, 
we receive a 200 in response so... 

if($asana->responseCode == "200" && !is_null($result)){ 
    $resultJson = json_decode($result); 

    var_dump($resultJson); 

} else { 
    echo "Error while trying to connect to Asana, response code: {$asana->responseCode}"; 
} 

다음은 클래스의 코드입니다.

<?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->apiKey = $apiKey; 

     $this->endPointUrl = "https://app.asana.com/api/{$this->asanaApiVersion}/"; 
     $this->taskUrl = $this->endPointUrl."tasks"; 
     $this->userUrl = $this->endPointUrl."users"; 
     $this->projectsUrl = $this->endPointUrl."projects"; 
     $this->workspaceUrl = $this->endPointUrl."workspaces"; 
     $this->storiesUrl = $this->endPointUrl."stories"; 
     $this->tagsUrl = $this->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->askAsana($this->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->askAsana($this->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" => "1768", 
    *  "name" => "Hello World!", 
    *  "notes" => "This is a task for testing the Asana API :)", 
    *  "assignee" => "176822166183", 
    *  "followers" => array(
    *   "37136", 
    *   "59083" 
    * ) 
    *) 
    * 
    * @return string JSON or null 
    */ 
    public function createTask($data){ 
     $data = array("data" => $data); 
     $data = json_encode($data); 
     return $this->askAsana($this->taskUrl, $data, METHOD_POST); 
    } 

    /** 
    * Returns task information 
    * 
    * @param string $taskId 
    * @return string JSON or null 
    */ 
    public function getTask($taskId){ 
     return $this->askAsana($this->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" => $data); 
     $data = json_encode($data); 
     return $this->askAsana($this->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->askAsana($this->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" => array("project" => $projectId)); 
     $data = json_encode($data); 
     return $this->askAsana($this->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" => array("project" => $projectId)); 
     $data = json_encode($data); 
     return $this->askAsana($this->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" => "", 
    *  "project" => 0, 
    *  "workspace" => 0 
    *) 
    * 
    * @return string JSON or null 
    */ 
    public function getTasksByFilter($filter = array("assignee" => "", "project" => "", "workspace" => "")){ 
     $url = ""; 
     $filter = array_merge(array("assignee" => "", "project" => "", "workspace" => ""), $filter); 
     $url .= $filter["assignee"] != ""?"&assignee={$filter["assignee"]}":""; 
     $url .= $filter["project"] != ""?"&project={$filter["project"]}":""; 
     $url .= $filter["workspace"] != ""?"&workspace={$filter["workspace"]}":""; 
     if(strlen($url) > 0) $url = "?".substr($url, 1); 

     return $this->askAsana($this->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->askAsana($this->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" => array(
       "text" => $text 
      ) 
     ); 
     $data = json_encode($data); 
     return $this->askAsana($this->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" => array("tag" => $tagId)); 
     $data = json_encode($data); 
     return $this->askAsana($this->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" => array("tag" => $tagId)); 
     $data = json_encode($data); 
     return $this->askAsana($this->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" => "1768", 
    *  "name" => "Foo Project!", 
    *  "notes" => "This is a test project" 
    *) 
    * 
    * @return string JSON or null 
    */ 
    public function createProject($data){ 
     $data = array("data" => $data); 
     $data = json_encode($data); 
     return $this->askAsana($this->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->askAsana($this->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 != "")?"&opt_fields={$opt_fields}":""; 
     return $this->askAsana($this->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->askAsana($this->projectsUrl."?archived={$archived}&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" => "Test", "notes" => "It's a test project"); 
    * 
    * @return string JSON or null 
    */ 
    public function updateProject($projectId, $data){ 
     $data = array("data" => $data); 
     $data = json_encode($data); 
     return $this->askAsana($this->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->askAsana($this->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->askAsana($this->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" => array(
       "text" => $text 
      ) 
     ); 
     $data = json_encode($data); 
     return $this->askAsana($this->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->askAsana($this->tagsUrl."/{$tagId}"); 
    } 

    /** 
    * Returns the full record for all tags in all workspaces. 
    * 
    * @return string JSON or null 
    */ 
    public function getTags(){ 
     return $this->askAsana($this->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" => "Test", "notes" => "It's a test tag"); 
    * 
    * @return string JSON or null 
    */ 
    public function updateTag($tagId, $data){ 
     $data = array("data" => $data); 
     $data = json_encode($data); 
     return $this->askAsana($this->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->askAsana($this->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->askAsana($this->storiesUrl."/{$storyId}"); 
    } 


    /** 
    * ********************************** 
    * Workspaces functions 
    * ********************************** 
    */ 

    /** 
    * Returns all the workspaces. 
    * 
    * @return string JSON or null 
    */ 
    public function getWorkspaces(){ 
     return $this->askAsana($this->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" => "Test"); 
    * 
    * @return string JSON or null 
    */ 
    public function updateWorkspace($workspaceId, $data = array("name" => "")){ 
     $data = array("data" => $data); 
     $data = json_encode($data); 
     return $this->askAsana($this->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->askAsana($this->taskUrl."?workspace={$workspaceId}&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->askAsana($this->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->askAsana($this->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->timeout); 
     curl_setopt($curl, CURLOPT_TIMEOUT, $this->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->apiKey); 
     curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
     curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); // Send as JSON 
     if($this->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) && ($method == METHOD_POST || $method == METHOD_PUT)){ 
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
     } 

     try { 
      $return = curl_exec($curl); 
      $this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 

      if($this->debug || $this->advDebug){ 
       echo "<pre>"; print_r(curl_getinfo($curl)); echo "</pre>"; 
      } 
     } catch(Exception $ex){ 
      if($this->debug || $this->advDebug){ 
       echo "<br>cURL error num: ".curl_errno($curl); 
       echo "<br>cURL error: ".curl_error($curl); 
      } 
      echo "Error on cURL"; 
      $return = null; 
     } 

     curl_close($curl); 

     return $return; 
    } 
} 

Here is Asana API doc

+1

대신 자신의 오류 메시지가 인쇄 : 여기

내가 작업 목록을 가져 오는 데 사용 정확한 코드 유용한 정보,이 클래스를 편집하고 디버그를 on으로 설정하면 진행 상황을 볼 수 있습니다. API 문서에 명시된 바와 같이 ** 400 ** 응답 코드는 요청이 잘못되었음을 의미합니다. –

답변

0

는 대부분의 경우 프로젝트 ID가 올바르지 않습니다. 알맞은 프로젝트 ID를 반향 출력하고 Asana에서 체크인하십시오 (Asana에서 프로젝트/작업을 볼 때 url의 첫 번째 긴 번호 임).

프로젝트 ID가 올바른지 확인한 후에 코드가 올바르게 작동해야합니다. 아래 코드가 나를 위해 작동하는지 확인했습니다. 이 null의 경우, 또는 포함하지 않는 경우, $의 result``에`var_dump`을 시도

 
    $tasks = $asana->getProjectTasks($project_ID); //got the $project_ID directly from the url in Asana. 
       $tasksJson = json_decode($tasks); 
       if($asana->responseCode == "200" && !is_null($tasks)){ 
        foreach ($tasksJson->data as $task){ 
         echo "+ " . $task->name . " (id " . $task->id . ")" . " ]
"; } } else { echo "Error while trying to connect to Asana, response code: {$asana->responseCode}"; }