Stream_context_create 的 https

vij*_*iji 5 php api magento2

我是这样尝试的新手,我试图在 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/>";
?>
Run Code Online (Sandbox Code Playgroud)

请帮忙,提前致谢。

小智 2

抱歉回复太晚了,但我也在寻找这个解决方案,使用 https URL 而不是 http。

从 PHP 手册页/示例来看,上下文不应该是 https,而应该是 http。:http://php.net/manual/it/function.stream-context-create.php#74795

错误:

$context_options = array (
    'https' => array ()
)
Run Code Online (Sandbox Code Playgroud)

正确的:

$context_options = array (
    'http' => array ()
)
Run Code Online (Sandbox Code Playgroud)

如果您需要更多 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'
    )
);
Run Code Online (Sandbox Code Playgroud)