gpt4 book ai didi

PHP cURL SSL 连接错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:25 25 4
gpt4 key购买 nike

我尝试使用 file_get_contents() 函数显示 SSL 网页的内容。

$url = base64_decode("aHR0cHM6Ly93d3cudGVudGtvdHRhLmNvbQ==");
echo file_get_contents($url);

$header = array("Connection: Keep-Alive", "User-Agent: Mozilla/5.0");
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
curl_setopt($ch, CURLOPT_SSLVERSION , 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$result = curl_exec($ch);
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($ch);
echo $result;

但它抛出一个警告说,

Warning: file_get_contents(): Failed to enable crypto in

然后我尝试使用 cURL 库。但它也给我一个 35 号错误,即 SSL 连接错误。

我在 cURL 网站上搜索过,它说连接网页时出错。

CURLE_SSL_CONNECT_ERROR (35)

A problem occurred somewhere in the SSL/TLS handshake. You really want the error buffer and read the message there as it pinpoints the problem slightly more. Could be certificates (file formats, paths, permissions), passwords, and others.

你能请任何人帮助我吗?

谢谢。

最佳答案

简单描述:

我修正了你的代码。这是一个工作示例:

$url = base64_decode("aHR0cHM6Ly93d3cudGVudGtvdHRhLmNvbQ==");

$header = array("User-Agent: Mozilla/5.0");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($ch);
echo $result;

更长的解释

在 base64 解码后 $url 等于 https://www.tentkotta.com ,它(当使用您问题中的代码请求时)返回 HTTP 500 Internal服务器错误:

https://www.tentkotta.com
HTTP/1.1 500 Internal Server Error
Date: Sat, 04 Jun 2016 02:15:19 GMT
Server: Apache
X-Powered-By: PHP/5.6.21
Set-Cookie: kohanasession=jfl6dh4k386htjpgmh99ns54t1; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: kohanasession=jfl6dh4k386htjpgmh99ns54t1; path=/
Set-Cookie: kohanasession_data=c2Vzc2lvbl9pZHxzOjI2OiJqZmw2ZGg0azM4Nmh0anBnbWg5OW5zNTR0MSI7dG90YWxfaGl0c3xpOjE7X2tmX2ZsYXNoX3xhOjA6e311c2VyX2FnZW50fHM6MDoiIjtpcF9hZGRyZXNzfHM6MTI6Ijk5LjE1MC4yNDUuNCI7bGFzdF9hY3Rpdml0eXxpOjE0NjUwMDY1MTk7TEFTVF9BQ1RJVklUWXxpOjE0NjUwMDY1MTk7Q2l0eUlEfHM6MjoiMTMiOw%3D%3D; path=/
Vary: Accept-Encoding,User-Agent
Content-Length: 828
Connection: close
Content-Type: text/html; charset=UTF-8

<link rel="stylesheet" type="text/css" href="https://www.tentkotta.com/themes/pink/css/style.css" />
<link rel="shortcut icon" href="https://www.tentkotta.com/themes/pink/images/favicon.ico" type="image/x-icon" />
<div class="container_outer" >
<div class="container_inner">
<div class="container">
<div class="content_fourty_four_page">
<div class="error_inner_page">
<div class="error_left_im">
<img src="https://www.tentkotta.com/themes/pink/images/404_error_img.png" alt="404 error" />
</div>
<div class="error_right_cont">
<img src="https://www.tentkotta.com/themes/pink/images/error_right_im.png" alt="404 error" />
<p>The page you’re looking for cannot be found.</p>
<a href="javascript:history.back();" title="Back">Back</a>
</div>
</div>

</div>
</div>
</div>
</div>

要停止在服务器端引起错误,您需要更改此行:

curl_setopt($ch, CURLOPT_HEADER, $header);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

CURLOPT_HEADER 用于响应 header 。 CURLOPT_HTTPHEADER 用于请求 header 。查看curl_setopt docs了解详情。

另外,删除这一行:

curl_setopt($ch, CURLOPT_SSLVERSION , 3);

由于 POODLE vulnerability,SSLv3 可能已在您的目标 URL 上被禁用.只需让 cURL 和主机协商最佳协议(protocol),而不是您自己指定。

此外,由于 https://www.tentkotta.com,以下行不是必需的似乎确实具有有效的 SSL 证书:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

此外,如果您希望 $result 变量包含 HTTP 请求的响应主体,您应该添加以下行:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

除此之外,您的许多其他设置(例如 "Connection: Keep-Alive"CURLOPT_TIMEOUTCURLOPT_IPRESOLVE 对此没有帮助例如,可以安全地删除。

关于PHP cURL SSL 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37625633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com