curl总是回复401 Unauthorized,但仅限Laravel

时间:2017-05-31 08:03:58

标签: php laravel curl icloud caldav

我有一个使用curl从远程服务器(来自icloud的用户/日历数据)中检索数据的类。当我使用单个文件时,一切都很完美,但如果我在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();
    }

我尝试了不同的卷曲设置变体(ssl开/关,选项/标题中的auth等等),但结果仍然相同。也试过Guzzle,但还是一样的。 但对我来说奇怪的是当我使用完全相同的代码时,只是将它包含在普通的php文件中,一切正常。

0 个答案:

没有答案