我试图通过从PHP发送cURL请求来使用Office 365(实际上是Sharepoint)文件REST API。但是我得到了以下错误:
Unknown SSL protocol error in connection to mysubdomain-my.sharepoint.com:443
我试图摆弄CURLOPT_SSLVERSION设置,我应该使用SSL3(3)或TLS1(1)设置。我还认为Officw 365 API已停止使用SSL3。
除此之外,我不知道如何解决这个问题。关于使用PHP连接到Microsoft的API的信息非常少。
我不确定使用mysubdomain.onmicrosoft.com/myapp作为资源(在下面的$ authenticationRequestBody中)。 WebApp-GraphAPI-PHP使用不同的资源字符串,但根据https://msdn.microsoft.com/en-us/library/azure/dn645543.aspx,应该使用与我的示例类似的内容。
这是源代码。基本上它是WebApp-GraphAPI-PHP example略有修改:
<?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);
?>