0

. 我将 laravel 后端用作 API,将 angular 用作调用 laravel api 的前端。Laravel api 通过使用 curl 的基本身份验证访问团队合作 api。

现在我正在使用团队合作 Api。尝试使用 API 创建评论。API 文档描述如下。

    {
  "comment": {
    "body": "Reply to earlier comment",
    "notify": "",
    "isprivate": false,
    "pendingFileAttachments": "",
    "content-type": "TEXT"
  }
}

//参考:http: //developer.teamwork.com/comments#creating_a_commen

在我的ajax调用中,我使用了以下

  var data = $.param({
    'body' : commentBodyValue,  //variable
    'notify': "",
    'isPrivate':false,
    "pendingFileAttachments": "",
    "content-type": "TEXT"
  });

我的帖子没有给出错误,但也没有创建新评论。我错过了什么?我想我没有按照 api 中允许的格式排列数据。你能帮我解决它吗?

编辑:

角度控制器:

//add new comment
$scope.addComment = function(taskId,commentAuthorId)
{
  var commentBod  = document.getElementById("commentBody");
  var commentBodyValue = commentBod.value;
  console.log("new comment : "+ taskId + commentBodyValue);
  //submit the post request
  //$http.post();
  //using jquery function param to serialize 
  var data = $.param({
    'body' : commentBodyValue,
    'notify': "",
    'isPrivate':false,
    "pendingFileAttachments": "",
    "content-type": "TEXT"
  });
  var config = {
      headers : {
          // 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;',
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
  }
  var url = "http://abounde.com/portal/api/post_comment/"+taskId;
  $http.post(url,data,config)
  .then(
    function(response){
      //success
      console.log("response : Submitted :D  " + response);
    },
    function(response){
      //failure
      console.log(response);
    }
  );

}

编辑 2

Laravel 路由和控制器 Routes: //向资源发表评论 Route::post('post_comment/{task_id}',[ 'uses'=>'PortalController@postComment', 'as'=>'portal.client.postComment' ] );

控制器:

    //post comment at task
        public function postComment(Request $request, $task_id)
        {
            $secretApiKey = $request->session()->get('secretApiKey');
            if(!empty($secretApiKey)){
            //post the msg
                $this->callTeamworkApi($secretApiKey,"tasks/".$task_id."/comments.json");

            }else{
                return response()->json(['response'=>"Not Logged In"]);
            }           
        }


//authentication method

    public function callTeamworkApi($secretApiKey, $apiCallString)
    {
            //cURL
        $password = "xxx";
        $channel = curl_init();
        //options
        curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
        curl_setopt($channel, CURLOPT_HTTPHEADER,
                    array(
                      "Authorization: Basic " . base64_encode($secretApiKey . ":" . $password)
        ));


        $msg = curl_exec($channel);
        curl_close($channel);
        return response()->json(['res'=>$msg]);

    }

编辑 3:与团队合作 Api 支持联系。根据他们的建议,我想出了以下代码

  //post comment at task
    public function postComment(Request $request, $task_id)
    {
        $secretApiKey = $request->session()->get('secretApiKey');
        if(!empty($secretApiKey)){

        //post the msg
            $comment=array();
            // $comment['body']=$request->input('body');
            // $comment['notify']=$request->input('notify');          
            // $comment['isprivate']=$request->input('isprivate');
            // $comment['pendingFileAttachments']=$request->input('pendingFileAttachments');
            // $comment['content-type']=$request->input('content-type');


            $comment['comment']['body']="test";
            $comment['comment']['notify']="";
            $comment['comment']['isprivate']=false;
            $comment['comment']['pendingFileAttachments']="";
            $comment['comment']['content-type']="text";


            $this->callTeamworkPostApi($secretApiKey,"tasks/".$task_id."/comments.json",json_encode($comment));
            //var_dump($comment);
            //return response()->json(['response'=>"Trying"]);
        }else{
            return response()->json(['response'=>"Not Logged In"]);
        }           
    }
}

///

  public function callTeamworkPostApi($secretApiKey, $apiCallString,$comment)
{
        //cURL
    $password = "xxx";
    $channel = curl_init();
    //options
    curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
    curl_setopt($channel, CURLOPT_HTTPHEADER,
                array(
                  "Authorization: Basic " . base64_encode($secretApiKey . ":" . $password)
    ));
    curl_setopt($channel, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($channel, CURLOPT_POSTFIELDS, $comment);


    $msg = curl_exec($channel);
    curl_close($channel);
            var_dump($msg);
    //return response()->json(['res'=>$msg]);


}

var_dump($comment) 给出

string(107) "{"comment":{"body":"test","notify":"","isprivate":false,"pendingFileAttachments":"","content-type":"text"}}"

但是 api 仍然不保存评论。它必须是一个 curl post 方法问题。正确的?

编辑 4:我做了一个 var_dump(curl_getinfo($channel));

这给了我以下回应。

array(26) { ["url"]=> string(55) "http://projects.abounde.com/tasks/8199861/comments.json" ["content_type"]=> NULL ["http_code"]=> int(500) ["header_size"]=> int(229) ["request_size"]=> int(311) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.5) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0.109) ["pretransfer_time"]=> float(0.109) ["size_upload"]=> float(107) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(214) ["download_content_length"]=> float(0) ["upload_content_length"]=> float(107) ["starttransfer_time"]=> float(0.5) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(13) "23.23.184.208" ["certinfo"]=> array(0) { } ["primary_port"]=> int(80) ["local_ip"]=> string(11) "192.168.0.6" ["local_port"]=> int(31657) }

响应代码 500 可能表明团队合作存在某种内部问题。

4

2 回答 2

1

我发现的问题是您没有将从前端收到的评论数据传递给 callTeamworkApi() 中的 curl 请求。尝试下面的代码,看看是否获得了所需的输出

//post comment at task
    public function postComment(Request $request, $task_id)
    {
        $secretApiKey = $request->session()->get('secretApiKey');
        if(!empty($secretApiKey)){

        //post the msg
            $comment=array();
            $comment['body']=$request->input('body');
            $comment['notify']=$request->input('notify');          
            $comment['isPrivate']=$request->input('isprivate');
            $comment['pendingFileAttachments']=$request->input('pendingFileAttachments');
             $comment['content-type']=$request->input('content-type');


            $this->callTeamworkApi($secretApiKey,"tasks/".$task_id."/comments.json",json_encode($comment));

        }else{
            return response()->json(['response'=>"Not Logged In"]);
        }           
    }


 //authentication method

public function callTeamworkApi($secretApiKey, $apiCallString,$comment)
{
        //cURL
    $password = "xxx";
    $channel = curl_init();
    //options
    curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
    curl_setopt($channel, CURLOPT_HTTPHEADER,
                array(
                  "Authorization: Basic " . base64_encode($secretApiKey . ":" . $password)
    ));
    curl_setopt($channel, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($channel, CURLOPT_POSTFIELDS, $comment);


    $msg = curl_exec($channel);
    curl_close($channel);
    return response()->json(['res'=>$msg]);

}
于 2016-08-22T14:53:21.813 回答
-1

找到的解决方案: 调用团队合作 API 时,必须将 content-type 设置为 Json。并且信息应该在头文件中。标头变量应该是一个包含身份验证和内容类型信息的多数组。

$headers = array();
$headers[] = "Authorization: Basic " . base64_encode($secretApiKey . ":" . $password);
$headers[] = "Content-Type: application/json";


    //pass it as variable
    curl_setopt($channel, CURLOPT_HTTPHEADER, $headers);

然后它就像魅力一样。

创建评论后返回以下内容。

{"commentId":"4294639","STATUS":"OK"}
于 2016-08-23T09:08:32.877 回答