我是尝试这样的新手,我试图在 magento2 中使用 https 获取令牌,它在这里不起作用我的代码
<?php
$base_url="https://myurl.com/";
$domain="myurl.com";
$url = $base_url.'index.php/rest/V1/integration/admin/token';
$body = '{"username":"ApiUser", "password":"ApiPass"}';
$context_options = array (
'https' => array (
'method' => 'POST',
'header'=> "Content-type: application/json"
'content' => $body
),
'ssl' => array('SNI_enabled'=>true,'SNI_server_name'=> $domain)
);
$context = stream_context_create($context_options);
$token = json_decode(file_get_contents($url, false, $context));
echo $token; echo "<br/>";
?>
请帮忙,提前谢谢。
很抱歉回复太晚了,但也只是在寻找这个解决方案,使用 https URL 而不是 http。
从 PHP 手册页/示例中,上下文不应该是 https,而是 http。 http://php.net/manual/it/function.stream-context-create.php#74795
不對:
$context_options = array (
'https' => array ()
)
正确:
$context_options = array (
'http' => array ()
)
如果您需要更多SSL选项,则此评论: http://php.net/manual/it/function.stream-context-create.php#110158
$contextOptions = array(
'ssl' => array(
'verify_peer' => true,
'cafile' => __DIR__ . '/cacert.pem',
'verify_depth' => 5,
'CN_match' => 'secure.example.com'
)
);
SSL
在环境中工作时,使用stream_context_create
创建流上下文的最简单方法是省略验证!当我想用file_get_contents
检索数据时,这对我来说很好用:
stream_context_create([
'http' => [ /* your options here - eg: */ 'method' => 'POST', ],
// 'https' => 'DON'T forget there is no "https", only "http" like above',
'ssl' => [ // here comes the actual SSL part...
'verify_peer' => false,
'verify_peer_name' => false,
]
]);
<小时 />php.net/manual/en/context.ssl.php:
verify_peer
boolean
要求验证所使用的 SSL 证书.
默认为 TRUE。
verify_peer_name
boolean
需要验证对等名称.
默认为 TRUE。