curl всегда отвечает с 401 Unauthorized, но только с Laravel

У меня есть класс, который получает данные с удаленного сервера (данные пользователя / календаря из icloud) с помощью curl. Все отлично работает, когда я использую один файл, но если я использую этот класс в Laravel (версия 5.4), я получаю HTTP/1.1 401 Unauthorized каждый раз.
Вот короткая версия кода класса:

<?php
class CalDavClient
{
protected $url;
protected $server;
protected $user;
protected $user_id;
protected $calendar_id;
protected $password;
protected $depth = 1;
protected $user_agent = "DAVKit/4.0.1 (730); CalendarStore/4.0.1 (973); iCal/4.0.1 (1374); Mac OS X/10.6.2 (10C540)";

public function __construct($server, $user, $password, $user_id = null)
{
$this->user = $user;
$this->password = $password;
$this->server = $server;
if(isset($user_id))
$this->user_id = $user_id;
}

public function setDepth($depth = null)
{
if (isset($depth))
$this->depth = $depth;
return $this->depth;
}

public function getUserId()
{
$principal_request = '<d:propfind xmlns:d="DAV:"><d:prop><d:current-user-principal /></d:prop></d:propfind>';
$url = $this->server;
$res = $this->doRequest($url, $principal_request);
$response=simplexml_load_string($res);
//Principal URL
$principal_url=$response->response[0]->propstat[0]->prop[0]->{'current-user-principal'}->href;
$userID = explode("/", $principal_url);
return $this->user_id = $userID[1];
}

private function doRequest($url, $xml)
{
//Init cURL
$c = curl_init();
curl_setopt_array($c, [
CURLOPT_URL => $url,
//Set headers
CURLOPT_HTTPHEADER => array(
'Authorization: Basic '. base64_encode("$this->user:$this->password"),
"Depth: " . $this->depth,
"Content-Type: text/xml; charset=utf-8",
"Content-Length: " . strlen($xml),
"User-Agent: DAVKit/4.0.1 (730); CalendarStore/4.0.1 (973); iCal/4.0.1 (1374); Mac OS X/10.6.2 (10C540)",
'X-Requested-With: XMLHttpRequest',
),
CURLOPT_HEADER => false,
CURLINFO_HEADER_OUT => true,
//Set SSL
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
//Set request and XML
CURLOPT_CUSTOMREQUEST => "PROPFIND",
CURLOPT_POSTFIELDS => $xml,
CURLOPT_RETURNTRANSFER => 1,
]);
//curl_setopt($c, CURLOPT_HEADER, 1);
//Execute
$error = curl_error($c);
$response = curl_exec($c);
$info = curl_getinfo($c);
//Close cURL
curl_close($c);
dd($error, $response, $info);
return $response;
}
}

И вот как это работает в моем контроллере:

public function test(Request $request) {
$client = new CalDavClient('https://p01-caldav.icloud.com', $request->user, $request->pass);
$client->getUserId();
}

Я пробовал разные варианты настроек curl (ssl on / off, auth в options / in headers и так далее …), но результат все тот же. Также пытаюсь жадно, но все же тоже самое.
Но что странно для меня, когда я использую точно такой же код, но просто включаю его в обычный php-файл, все работает нормально.

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …