php curl rest api token return false Magento 2

php curl rest api token return false Magento 2

我正在尝试使用 Magento 2 rest API 获取授权令牌,代码如下

$userData = array("username" => "user", "password" => "password");
$ch = curl_init("https://domain/index.php/rest/V1/integration/admin/token");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);
var_dump($token);

它在本地服务器上运行良好,但在测试服务器上 $token = false。我可以从控制台使用 curl 检索令牌。

curl -X POST "https://domain/index.php/rest/V1/integration/admin/token/" -H "Content-Type:application/json" -d '{"username":"user", "password":"password"}'

为什么我在 PHP 脚本中收到 false?有任何想法吗?

curl_error($ch) 的输出给我以下错误

SSL certificate problem: unable to get local issuer certificate

轻松修复 - 。在这里复制它

1) Download the latest cacert.pem from https://curl.haxx.se/ca/cacert.pem

2) Add the following line to php.ini (if this is shared hosting and you don't have access to php.ini then you could add this to .user.ini in public_html)

curl.cainfo=/path/to/downloaded/cacert.pem