PHP cURL SSL连接错误

时间:2016-06-04 01:59:12

标签: php ssl curl

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

printf("n[%d] %s\n", n, token_array[n]);

但它给我一个警告说,

$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;

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

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

  

CURLE_SSL_CONNECT_ERROR(35)

     

SSL / TLS握手中出现问题。你真的想要错误缓冲区并在那里阅读消息,因为它会稍微查明问题。可以是证书(文件格式,路径,权限),密码等。

你能不能请任何人帮助我。

谢谢。

1 个答案:

答案 0 :(得分:1)

TLDR:

我修复了你的代码。这是一个有效的例子:

$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内部服务器错误:

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用于响应标头。 CURLOPT_HTTPHEADER用于请求标头。有关详细信息,请参阅curl_setopt docs

另外,请删除此行:

curl_setopt($ch, CURLOPT_SSLVERSION , 3);

由于POODLE vulnerability,您的目标网址可能已停用SSLv3。只需让cURL和主机协商最佳协议,而不是自己指定协议。

此外,由于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等许多其他设置在此示例中没有帮助,可以安全删除。