Jam*_*son 99 php https curl file-get-contents
我正在设置信用卡处理,需要使用CURL的解决方法.当我使用测试服务器(没有调用SSL URL)时,以下代码工作正常,但现在当我在使用HTTPS的工作服务器上测试它时,它失败并显示错误消息"无法打开流".
function send($packet, $url) {
$ctx = stream_context_create(
array(
'http'=>array(
'header'=>"Content-type: application/x-www-form-urlencoded",
'method'=>'POST',
'content'=>$packet
)
)
);
return file_get_contents($url, 0, $ctx);
}
Run Code Online (Sandbox Code Playgroud)
小智 113
要允许https包装器:
php_openssl扩展必须存在且已启用allow_url_fopen 必须设置为 on在php.ini文件中,如果不存在,则应添加以下行:
extension=php_openssl.dll
allow_url_fopen = On
Run Code Online (Sandbox Code Playgroud)
Vol*_*erK 87
尝试使用以下脚本来查看是否有可用于php脚本的https包装器.
$w = stream_get_wrappers();
echo 'openssl: ', extension_loaded ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_export($w);
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的
openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: array(11) {
[...]
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*ier 51
请尝试以下方法.
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Run Code Online (Sandbox Code Playgroud)
注意:这会禁用SSL验证,这意味着HTTPS提供的安全性会丢失.仅使用此代码进行测试/本地开发,绝不使用互联网或其他面向公众的网络.如果此代码有效,则表示SSL证书不受信任或无法验证,您应将其视为单独的问题.
小智 38
$url= 'https://example.com';
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents($url, false, stream_context_create($arrContextOptions));
Run Code Online (Sandbox Code Playgroud)
这将允许您从网址获取内容,无论它是否为HTTPS
如果您已编译支持OpenSSL,则从PHP 4.3.0开始支持HTTPS.此外,确保目标服务器具有有效证书,防火墙允许出站连接,并且allow_url_fopen在php.ini中设置为true.