PHP and Office 365: Unknown SSL Protocol Error

I am trying to use the Office 365 (actually Sharepoint) files REST API by sending cURL requests from PHP. However, I got the following error:

Unknown SSL protocol error in connection to mysubdomain-my.sharepoint.com:443

      

I was trying to tinker with the CURLOPT_SSLVERSION option, from what I read the SSL3 (3) or TLS1 (1) settings should be used. I also read that Officw 365 API has stopped using SSL3.

Other than that, I don't know what to do to fix this problem. There is very little information on using PHP to connect to Microsoft APIs.

I'm not sure about using mysubdomain.onmicrosoft.com/myapp as a resource (in the $ authenticationRequestBody below). WebApp-GraphAPI-PHP uses a different resource string, but according to https://msdn.microsoft.com/en-us/library/azure/dn645543.aspx something similar to my example should be used.

Here is the source code. Basically this is a WebApp-GraphAPI-PHP example slightly modified:

<?php
$TENANT = "MY SUB DOMAIN";
$TENANT_DOMAIN = "$TENANT.onmicrosoft.com";
$APP_ID = "APP ID";
$SECRET = "CLIENT SECRET";
$APP_NAME = "APP NAME";

//Generate the authentication header
$clientSecret = urlencode($SECRET);
// Information about the resource we need access for which in this case is graph.
    $graphId = "https://" . $TENANT_DOMAIN . "/" . $APP_NAME;
$graphPrincipalId = urlencode($graphId);
// Information about the app
$clientPrincipalId = urlencode($APP_ID);

// Construct the body for the STS request
$authenticationRequestBody = 'grant_type=client_credentials&client_secret='.$clientSecret
                           .'&'.'resource='.$graphPrincipalId.'&'.'client_id='.$clientPrincipalId;

//Using curl to post the information to STS and get back the authentication response    
$ch = curl_init();
// set url 
$stsUrl = 'https://login.windows.net/' . $TENANT_DOMAIN . '/oauth2/token?api-version=1.0';        
curl_setopt($ch, CURLOPT_URL, $stsUrl); 
// Get the response back as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
// Mark as Post request
curl_setopt($ch, CURLOPT_POST, 1);
// Set the parameters for the request
curl_setopt($ch, CURLOPT_POSTFIELDS,  $authenticationRequestBody);

// By default, HTTPS does not work with curl.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// read the output from the post request
$output = curl_exec($ch);         
// close curl resource to free up system resources
curl_close($ch);

// decode the response from sts using json decoder
$tokenOutput = json_decode($output);

$authHeader = 'Authorization:' . $tokenOutput->{'token_type'}.' '.$tokenOutput->{'access_token'};

$ch = curl_init();

// Add authorization header, request/response format header( for json) and a header to request content for Update and delete operations.  
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authHeader,  'Accept:application/json;odata=minimalmetadata',
                                           'Content-Type:application/json;odata=minimalmetadata', 'Prefer:return-content'));
// Set the option to recieve the response back as string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
// By default https does not work for CURL.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_URL, "https://$TENANT-my.sharepoint.com/_api/v1.0/me/files/root/children");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);

$output = curl_exec($ch);
echo(curl_error($ch));
// close curl resource to free up system resources 
curl_close($ch);

var_dump($output);
?>

      

+3


source to share





All Articles