php file_get_contents openssl,使用file_get_contents调用HTTPS API

本文探讨了正式服PHP应用无法通过file_get_contents访问HTTPS API的问题,原因在于缺少php_openssl.dll扩展。给出了两种解决方案:一是安装该扩展,二是利用curl进行安全请求。
摘要由CSDN通过智能技术生成

问题描述

正式服使用file_get_contents访问API(HTTPS)接口时返回false,而测试服使用正常。

排查原因

正式服未安装php_openssl.dll扩展。

解决方案

1.安装php_openssl.dll扩展

2.使用curl进行API调用

// 使用file_get_contents()进行HTTPS API调用

public function api() {

$option = array (

'http' => array (

'header' => "Your_Header_Name: Your_Header_Value"

)

);

$url = 'https://www.baidu.com';

$res = file_get_contents ( $url, false, stream_context_create ( $option ) );

$result = json_decode ( $res, true );

}

// 使用curl()进行HTTPS API调用

public function api() {

$url = 'https://www.baidu.com'

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $site.$url);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($curl, CURLOPT_HTTPHEADER, array(

'Your_Header_Name: Your_Header_Value'

));

if (!empty($data)) {

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

}

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// 结果为json字符串

$info = curl_exec($curl);

curl_close($curl);

$result = json_decode($info,true);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值