0

Since a few weeks, I cannot access to the OneDrive Personal API using cURL in PHP. The same URL works fine in a browser.

The URL for OneDrive Personal API looks like:

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

Here's the cURL code I actually use, and worked since:

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;
}

In debug mode, the error message is "Failed to connect to api.onedrive.com port 443: Connection refused"

Any idea?

3
  • Could be an outbound firewall rule? Strange that port 443 would be blocked, but it's worth checking
    – ADyson
    May 26, 2023 at 8:04
  • Are you using outdated protocol in your particular environment ? For your information, many outdated security protocols are access disabled in recent months this year (2023)
    – Ken Lee
    May 26, 2023 at 8:50
  • @KenLee that could be a possibility. However I don't find any documentation concerning those protocols or concerning OneDrive's ones. May 27, 2023 at 6:05

1 Answer 1

0

The most likely reason may be about my web host OVH. Since several weeks, I cannot display extensions or themes to add directly from the dashboard. There could be a firewall or smth similar, and that may explain why other queries like OneDrive may not work too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.