Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bPanda's avatar

curl request always respond with 401 Unauthorized but only with Laravel

I have a class that retreiving data from remote server (user/calendar data from icloud) using curl. Everything works perfectly, when i'm using single file (including this class into single file, written in native php), but if i use this class in Laravel (ver. 5.4) i'm receiving HTTP/1.1 401 Unauthorized every time. Here is short version of class code:

    <?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;
        }
    }

And this is how it works in my controller:

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

I've tried different variations of curl settings (ssl on/off, auth in options/in headers and so on...), but result still the same. Also trying Guzzle, but still same thing. But what is weird for me is when i'm using exact same code, but just including it into regular php file everything working fine. I'm totally free to any suggestions about this, cuz i'm out of ideas.

0 likes
1 reply
prabakar's avatar

just try cache:clear and config:cache, may be this will resolve the issue!

Please or to participate in this conversation.