使用HackerRank API将测试邀请发送给候选人

时间:2016-09-06 11:06:22

标签: php api curl

我在我的网站上使用HackerRank作为测试评估。我正在使用它的API将邀请发送给我的用户进行测试。我在PHP中使用CURL调用它的API,但是我收到错误“无效访问令牌”。

我使用此代码。

function httpPost($url,$params)
{
  $postData = '';
   //create name value pairs seperated by &
   foreach($params as $k => $v) 
   { 
      $postData .= $k . '='.$v.'&'; 
   }
   $postData = rtrim($postData, '&');
    $arr = array();
    $ch = curl_init();  

     curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type" => "application/x-www-form-urlencoded",
    "Accept" => "application/json"
    ));
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
    curl_setopt($ch,CURLOPT_MAXREDIRS,2);//only 2 redirects
    curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

    $output=curl_exec($ch);
    if($output === false)
    {
        $arr[]= "Error Number:".curl_errno($ch)."<br>";
        $arr[]= "Error String:".curl_error($ch);
    }
    curl_close($ch);
    $arr[]= $output;
    return $arr;

}

$data = array(
    'test_id'=> test_id_integer,
    'username'=> "EMAIL_ID",
    'access_token'=> 'API_KEY'
);

var_dump(httpPost("www.hackerrank.com/x/api/v2/tests/test_id_integer/candidates",$data));

0 个答案:

没有答案