使用 PHP-Curl 将文件上传到 Office 365 Onedrive 商业帐户

Upload file to Office 365 Onedrive Business account using PHP-Curl

我正在尝试使用 cURL 将 JSON 文件上传到 OneDrive Business AC 帐户中的新文件夹。上传时,出现以下错误:

HTTP status code not expected - got - 401

这是我的代码:

$uri = "https://graph.microsoft.com/v1.0/me/drive/root/new/sample.json/content?access_token=accesstoken";

function curl_put($uri, $fp) {
    $output = "";

    try {
        $pointer = fopen($fp, 'r+');

        $stat = fstat($pointer);
        $pointersize = $stat['size'];
        $ch = curl_init($uri);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_PUT, true);
        curl_setopt($ch, CURLOPT_INFILE, $pointer);
        curl_setopt($ch, CURLOPT_INFILESIZE, (int) $pointersize);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
    }
}

https://graph.microsoft.com/v1.0/me/drive/root:/foldername/filename:/content

将 header 传递为:

$headers = array( 'Content-Type: application/text', "Cache-Control: no-cache", "Pragma: no-cache", "Authorization: bearer ".$token );

并使用file_get_contents传递文件中的数据。