Search code examples
phpexchange-serverexchangewebservices

How to connect to the exchange 2013 EWS via php (just get photo)


How to connect to the exchange 2013 EWS just get photo ? What Library do I need (API) and how to embed it ? (I 'm beginner in php)

Here's a code I have now:

https://exchange.domen.local/ews/exchange.asmx/s/GetUserPhoto?email=mail@mail.ru&size=HR240x240

He ask me login/password. That's good. But I need a way to write the Login/password to the script. Thanks.


Solution

  • Here is how I handled this in PHP using curl. It is simple with no dependencies outside of curl.

    Tested against an Exchange 2013 server. Saves to a file directly.

    $server = ''; // owa.whatever.com, etc.
    $user = ''; // username without domain info
    $password = '';
    $email_to_get = ''; // Email to pull photo
    $fullurl = "https://$server/ews/Exchange.asmx/s/GetUserPhoto?email=$email_to_get&size=HR648x648"; //sizes defined at https://msdn.microsoft.com/en-us/library/jj194329(v=exchg.80).aspx
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $fullurl);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM | CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $returned = curl_exec($ch);
    
    $fp = fopen("pic.jpg", 'w'); // Save picture locally to .jpg
    fwrite($fp, $returned);
    fclose($fp);
    
    header('Content-type: image/jpeg');
    echo $returned; // Display the image on the page if desired