使用PHP-Curl将文件上载到Office 365 Onedrive Business帐户

时间:2017-11-13 10:42:57

标签: php curl office365 onedrive

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

  

不期望HTTP状态代码 - 得到 - 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);
    }
}

1 个答案:

答案 0 :(得分:1)

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

将标题传递为:

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

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