OneDrive 个人 API、PHP 和 cURL

问题描述 投票:0回答:1

几周以来,我无法在 PHP 中使用 cURL 访问 OneDrive Personal API。相同的 URL 在浏览器中工作正常。

OneDrive 个人 API 的 URL 如下所示:

https://api.onedrive.com/v1.0/shares/u!aHR0cHM6Ly8xZHJ2Lm1zL2YvcyFBaEEwRHgzcmxhUTRwWnB6N0h6RkZvQkxmR1Ywb1E=/root?expand=children

这是我实际使用的 cURL 代码,并且从那时起就开始工作:

function url_get_contents($url, $debug = false, $headers = false, $post = false) {

    $follow_redirects = true;

    $cookiefile = './cookie.txt';
    $cookiejar = './cookiejar.txt';

    $useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

    // initialise the CURL library
    $ch = curl_init();

    $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: fr,en-us,en;q=0.5";
    $header[] = "Pragma: "; //browsers keep this blank. 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);

    // specify the URL to be retrieved
    
    if ($post) {
        $args = substr($url, strpos($url, '?') + 1);
        $url = substr($url, 0, strpos($url, '?'));
        $fields = explode('&', $args);
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    }
    curl_setopt($ch, CURLOPT_URL, $url);

    // we want to get the contents of the URL and store it in a variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // specify the useragent: this is a required courtesy to site owners
    curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
    // ignore SSL errors
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/bot.html');
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);

    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);




    // return headers as requested
    if ($headers == true) {
        curl_setopt($ch, CURLOPT_HEADER, 1);
    }

    // only return headers
    if ($headers == 'headers only') {
        curl_setopt($ch, CURLOPT_NOBODY, 1);
    }

    // follow redirects - note this is disabled by default in most PHP installs from 4.4.4 up
    if ($follow_redirects == true) {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    }

    $content = curl_exec($ch);


    $hex = bin2hex($content);
    if (substr($hex, 0, 6) == '1f8b08') //GZip
        $content = gzdecode($content);


    // if debugging, return an array with CURL's debug info and the URL contents
    $info = curl_getinfo($ch);

    if ($debug == true) {
        $result['contents'] = $content;
        $result['info'] = $info;
        $result['info']['error'] = curl_error($ch);

        if (!$info['http_code'])
            $result['contents'] = fgc($url, $header);
    }



    // otherwise just return the contents as a variable
    else {
        $result = $content;

        if (!$info['http_code'])
            $result = fgc($url, $header);
    }


    // free resources
    curl_close($ch);
    
    @unlink($cookiejar);

    // send back the data
    return $result;
}

在调试模式下,错误消息为 “无法连接到 api.onedrive.com 端口 443:连接被拒绝”

有什么想法吗?

php curl onedrive
1个回答
0
投票

最可能的原因可能与我的网络主机OVH有关。几周以来,我无法显示直接从仪表板添加的扩展或主题。可能存在防火墙或类似的东西,这可以解释为什么其他查询(例如 OneDrive)也可能不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.