简体   繁体   English

为什么 PHP 脚本中没有执行 curl 代码? (WAMP 服务器)

[英]Why is the curl code not executed in PHP script? (WAMP Server)

I am trying to get a token to use the Microsoft Graph API ( https://docs.microsoft.com/en-us/graph/auth-v2-user?context=graph%2Fapi%2F1.0&view=graph-rest-1.0 ) via Curl.我正在尝试获取令牌以使用 Microsoft Graph API ( https://docs.microsoft.com/en-us/graph/auth-v2-user?context=graph%2Fapi%2F1.0&view=graph-rest- 1.0 ) 通过 Curl。 I have set up a simple Php file with this function:我用这个 function 设置了一个简单的 Php 文件:

function getToken() {
echo "start gettoken";
var_dump(extension_loaded('curl'));
$jsonStr = http_build_query(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);

 }

However, what I want to know is why the curl request is not working.但是,我想知道为什么 curl 请求不起作用。 Also the echo after the curl codeblock is not being executed, while 'start gettoken' is. curl 代码块之后的回声也没有被执行,而 'start gettoken' 是。 PHP_curl is enabled in my WAMP.我的 WAMP 中启用了 PHP_curl。 Why is this?为什么是这样?

Are you sure CURL is enabled because that code you have posted is ok and giving echo response before and after curl execution.您确定 CURL 已启用吗,因为您发布的代码没有问题,并且在执行 curl 之前和之后给出回显响应。

you're sending the token request in a JSON-format, and then you're lying to the server saying it's application/x-www-form-urlencoded -encoded when it's actually application/json -encoded, since these 2 formats are completely incompatible, the server fails to parse it.您正在以 JSON 格式发送令牌请求,然后您在向服务器撒谎说它是application/x-www-form-urlencoded -encoded,而实际上它是application/json -encoded,因为这两种格式是完全不兼容,服务器无法解析它。 and... ideally it should have responded HTTP 400 bad request (because your request can't be parsed as x-www-form-urlencoded)并且...理想情况下,它应该响应HTTP 400 bad request (因为您的请求无法解析为 x-www-form-urlencoded)

anyhow, to actually send it in the application/x-www-form-urlencoded -format, replace json_encode() with http_build_query()无论如何,要以application/x-www-form-urlencoded格式实际发送它,请将 json_encode() 替换为 http_build_query()

also get rid of the "Content-Length:" -header, it's easy to mess up (aka error-prone) if you're doing it manually (and indeed, you messed it up! there's supposed to be a space between the : and the number, you didn't add the space, but the usual error is supplying the wrong length), but if you don't do it manually, then curl will create the header for you automatically, which is not error-prone.还要摆脱"Content-Length:" header,如果您手动执行它很容易搞砸(也容易出错)(实际上,您搞砸了!应该有一个空格:和数字,您没有添加空格,但通常的错误是提供错误的长度),但如果您不手动执行,则 curl 将自动为您创建 header,这容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM