file_get_contents无法获取到https问题的解决方法

示例代码:

$text = "有人在您的博客发表了评论";
$desp = "**".$comment['author']."** 在 [「".$post->title."」](".$post->permalink." \"".$post->title."\") 中说到: \n\n > ".$comment['text'];
$postdata = http_build_query(
    array(
        'text' => $text,
        'desp' => $desp
        )
    );
$opts = array('http' =>array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
        )
    );
$context  = stream_context_create($opts);
$result = file_get_contents('https://www.iyuu.cn/'.$sckey.'.send', false, $context);
return  $comment;

以上这段代码,获取不到内容,提交会失败。

解决方法

添加以下参数就可以了

"ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        )

完整代码:

$text = "有人在您的博客发表了评论";
$desp = "**".$comment['author']."** 在 [「".$post->title."」](".$post->permalink." \"".$post->title."\") 中说到: \n\n > ".$comment['text'];
$postdata = http_build_query(
    array(
        'text' => $text,
        'desp' => $desp
        )
    );
$opts = array('http' =>array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
        ),
        // 解决SSL证书验证失败的问题
        "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        )
    );
$context  = stream_context_create($opts);
$result = file_get_contents('https://www.iyuu.cn/'.$sckey.'.send', false, $context);
return  $comment;
最后修改:2019 年 08 月 30 日 10 : 33 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论