我正在尝试通过Curl获取令牌以使用Microsoft Graph API(https://docs.microsoft.com/en-us/graph/auth-v2-user?context=graph%2Fapi%2F1.0&view=graph-rest-1.0)。我已经使用此功能设置了一个简单的Php文件:
function getToken() {
echo "start gettoken";
var_dump(extension_loaded('curl'));
$jsonStr = json_encode(Array(
"client_id" => "***",
"scope" => "https://graph.microsoft.com/.default",
"client_secret" => "***",
"grant_type" => "client_credentials"
));
$headers = Array("Content-Type: application/x-www-form-urlencoded", "Content-Length:" . strlen($jsonStr));
$ch = curl_init("https://login.microsoftonline.com/***.onmicrosoft.com/oauth2/v2.0/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$token = curl_exec($ch);
echo "test after curl";
return $token;
curl_error($ch);
}
但是,我想知道的是为什么curl请求不起作用。同样,curl代码块之后的回显没有被执行,而'start gettoken'被执行。在我的WAMP中启用了PHP_curl。为什么会这样?
您确定启用了CURL是因为您发布的代码是正确的,并且在执行curl之前和之后都给出了回显响应。