curl php ssl连接超时

时间:2012-06-29 18:40:11

标签: php curl

我在互联网上找到了这个代码,我修改了它供我使用,我不知道我在这里做错了什么,我收到了这个错误 卷曲错误:SSL连接超时 登录部分成功,但搜索不适合我。请问有人帮我吗?

<?php
//create array of data to be posted
$post_data['username'] = 'user';
$post_data['password'] = 'log';
$post_data['cmd'] = 'log';

//create array of data to be posted
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =
  curl_init('https://sie.com');
//set options
///curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 3990);

curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array(
    'Connection: Keep-Alive',
    'Keep-Alive: 300'
));

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request

//print_r(curl_getinfo($curl_connection));
//echo curl_errno($curl_connection) . '-' .
  //              curl_error($curl_connection);
//close the connection
//curl_close($curl_connection);


echo $result."\n";


$post_data1['cmd'] = 'Viewr';
$post_data1['search'] = 'test';



foreach ( $post_data1 as $key => $value1) {
    $post_items1[] = $key . '=' . $value1;
}
$post_string1 = implode ('&', $post_items1);
echo $post_string1."\n";
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result1 = curl_exec($curl_connection);
//show information regarding the request
//$result1 =1;
//close the connection
//curl_close($curl_connection);
if ($result1)
echo "ok \n\n";
else
echo "nok\n";
if(curl_errno($curl_connection))
{
    echo 'Curl error: ' . curl_error($curl_connection)."\n";
}
echo($post_string1);
echo $result1."\n";


curl_close($curl_connection);


?>

3 个答案:

答案 0 :(得分:5)

尝试添加此(但不要在生产中设置此选项!)

curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);

如果之后有效,那是因为CURL无法协商SSL证书。

请按照本教程获取有关如何获取CA证书的步骤:http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

要查看的其他内容是之前你的curl_exec(),curl_copy_handle();,然后设置其他参数。

此外,您也可以使用curl_init($url);重新启动cURL。

答案 1 :(得分:0)

使用我列出的2个选项暂时禁用SSL验证,可能(如果证书设置不正确)。请注意“CURLOPT_SSL_VERIFYHOST”选项。否则我会更多地使用这些选项。此外,您不必遍历这些值,您只需使用关联数组。

<?php
//create array of data to be posted
$post_data['username'] = 'user';
$post_data['password'] = 'log';
$post_data['cmd'] = 'log';

//create cURL connection
$curl_connection = curl_init();
//set options
curl_setopt($curl_connection,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curl_connection,CURLOPT_SSL_VERIFYPEER, false);

// ...

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);//it can accept associative arrays

// ...

?>

答案 2 :(得分:0)

在尝试其他答案后,我仍然收到SSL connection timeout错误。

我检查了php.ini并决定检查是否有与OpenSSL相关的内容,我确实发现:

;extension=php_openssl.dll

我通过取消注释该行来启用它。

extension=php_openssl.dll

现在,启用OpenSSL(这是Windows上的参数,在Linux / Mac上应该类似),重新启动我的Apache服务器后,我终于可以执行我的cURL请求而不会出现任何错误!