使用PHP-Curl将文件上传到Office 365 Onedrive Business帐户 [英] Upload file to Office 365 Onedrive Business account using PHP-Curl

查看:57
本文介绍了使用PHP-Curl将文件上传到Office 365 Onedrive Business帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to upload a JSON file to a new folder in OneDrive Business AC account using cURL. While uploading, I am getting the following error:

不需要HTTP状态代码-得到-401

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

将标头传递为:

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

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

and pass the data from the file using file_get_contents.

这篇关于使用PHP-Curl将文件上传到Office 365 Onedrive Business帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆