phpphp-curlexchange-server-2013

Get User Photo from Exchange server using PHP CURL


I am trying to get user profile picture located in Microsoft Exchange Server [version 2013].

I can see the picture using below url [Example] :

https://companydomain.com/owa/service.svc/s/GetPersonaPhoto?email=user1%40companydomain.com&size=HR96x96&sc=1464941029314

Requirement:

To get all the company user's profile picture downloaded to a folder.

What I have done so far?

Written a Script using Php CURL

<?php 
    $user = 'user1@companydomain.com';
    $password = 'XXXXXXX';
    $fullurl="https://companydomain.com/owa/service.svc/s/GetPersonaPhoto?email=user1%40companydomain.com&size=HR96x96&sc=1464941029314";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_FAILONERROR, 0);
    curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
    curl_setopt($ch, CURLOPT_URL, $fullurl);
    $returned = curl_exec($ch);
    curl_close ($ch);

Output: A page containing: Object moved to here. as text and when I clicked on the link "here" it goes to login page of Microsoft Outlook.

Please help me to get desired out put and let me know what I am missing.

Thanks in advance


Solution

  • It seems that you get redirected (301 or 302) at the URL you rovided. I can provide a solution, qhich provides you the "target"-URL which should be the needed Image.

        function get_web_page( $url ) 
    { 
        $options = array( 
            CURLOPT_RETURNTRANSFER => true,     // return web page 
            CURLOPT_HEADER         => true,    // return headers 
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects 
            CURLOPT_ENCODING       => "",       // handle all encodings 
            CURLOPT_USERAGENT      => "boss", // who am i 
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect 
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect 
            CURLOPT_TIMEOUT        => 120,      // timeout on response 
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects 
        ); 
    
        $ch      = curl_init( $url ); 
        curl_setopt_array( $ch, $options ); 
        $content = curl_exec( $ch ); 
        $err     = curl_errno( $ch ); 
        $errmsg  = curl_error( $ch ); 
        $header  = curl_getinfo( $ch ); 
        curl_close( $ch ); 
    
        //$header['errno']   = $err; 
       // $header['errmsg']  = $errmsg; 
        //$header['content'] = $content; 
        print($header[0]); 
        return $header; 
    }  
    $thisurl = "http://www.example.com/redirectfrom";
    $myUrlInfo = get_web_page( $thisurl ); 
    echo $myUrlInfo["url"];
    

    you should get now http://www.example.com/redirectto