コード例 #1
0
 protected function gitApiCall($data, $gitAPIcommand = '', $method = 'GET')
 {
     $jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
     GeneralMethods::OutputToScreen('Running Git API command ' . $gitAPIcommand . ' using ' . $method . ' method...');
     $gitUserName = $this->Config()->get('git_user_name');
     $url = 'https://api.github.com/:repos/' . trim($gitUserName) . '/:' . trim($this->ModuleName);
     if (trim($gitAPIcommand)) {
         $url .= '/' . trim($gitAPIcommand);
     }
     $method = trim(strtoupper($method));
     $ch = curl_init($url);
     $header = "Content-Type: application/json";
     if ($method == 'GET') {
         $url .= '?' . http_build_query($data);
     }
     $gitApiUserName = trim($this->Config()->get('git_api_login_username'));
     $gitApiUserPassword = trim($this->Config()->get('git_api_login_password'));
     $gitApiAccessToken = trim($this->Config()->get('git_personal_access_token'));
     if (trim($gitApiAccessToken)) {
         $gitApiUserPassword = $gitApiAccessToken;
     }
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
     curl_setopt($ch, CURLOPT_USERAGENT, 'Silverstripe-update-module-module');
     if (isset($gitApiUserName) && isset($gitApiUserPassword)) {
         curl_setopt($ch, CURLOPT_USERPWD, $gitApiUserName . ':' . $gitApiUserPassword);
         curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
     }
     if ($method == 'POST') {
         curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
     }
     $curlResult = curl_exec($ch);
     if (!$curlResult) {
         die('Curl failed.');
     }
     print_r($url);
     print_r('<br/>');
     print_r($curlResult);
     die;
     return $curlResult;
 }