PHP Curl& HTTPS无效

时间:2017-10-31 18:27:34

标签: php curl https

所以我使用PHP和Curl尝试下载https weburl(来自https://www.g2crowd.com)但它不起作用,这是我到目前为止的一个简单的卷曲和另一个更高级的卷曲的代码,两者都不工作SADL。 :(

// connect via SSL, but don't check cert
$ch=curl_init('https://www.g2crowd.com');
curl_setopt ($ch, CURLOPT_CAINFO, "cacert.pem");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);  

$content = curl_exec($ch);

echo $content; // show target page
$ckfile = tempnam ("/tmp", 'cookiename');

$url='https://www.g2crowd.com/';
//$url='https://www.google.com';

$ch = curl_init();
curl_setopt( $ch, CURLOPT_COOKIESESSION, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $ckfile );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $ckfile ); 

curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = [
'Host: www.g2crowd.com',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: gzip, deflate'    
];
curl_setopt($ch, CURLOPT_CAINFO, 'C:\Users\ivan\Downloads\cacert.pem');

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, "https://www.g2crowd.com/users/c190d528-ab02-4cb5-8467-9362ceaec290");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$server_output = curl_exec ($ch);
if(curl_error($ch))
{
    echo curl_error($ch);
}
else {
    echo 'eureka!';
}

curl_close ($ch);

我试图捕获卷曲错误,所以这是错误:

1407742E:SSL例程:SSL23_GET_SERVER_HELLO:tlsv1警报协议版本

请帮助我真的在我的智慧结束!

感谢你!!

2 个答案:

答案 0 :(得分:1)

您的openssl计划已过期。我也在OSX版本上看到了这一点。如果您使用的是Mac,则需要下载其他openssl。或者,使用0打开SSL意味着您在没有SSL的情况下打开。

答案 1 :(得分:0)

这可以解决您的问题:

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

$ch = curl_init();

curl_setopt( $ch, CURLOPT_AUTOREFERER, TRUE );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );

$server_output = curl_exec( $ch );

curl_close( $ch );

如果不起作用,请尝试:

$url='https://www.g2crowd.com/';
$server_output = file_get_contents( $url );