使用旧证书调用 HTTPS 页面时的 cURL 超时

2023-06-22php开发问题
11

本文介绍了使用旧证书调用 HTTPS 页面时的 cURL 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 PHP 和 cURL (libcurl) 从我所在机构的数据服务获取 XML.开发正在我的本地机器上完成.它是在 PHP 中作为 Drupal 和 Transformations 模块的一部分进行评估的代码.

I am trying to get the XML from a Data Service at my institution using PHP and cURL (libcurl). The development is being done on my local machine. It is code that is eval'd in PHP as part of Drupal and the Transformations module.

它具有 SSL 支持,如运行所示:

It has SSL support as shown from running:

$curl-config --features (来自 libcurl 文档)

SSL
IPv6

NTLM

SSL
IPv6
libz
NTLM

正在执行的 PHP 代码:

The PHP code being executed:

/**
 * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
 * array containing the HTTP server response header fields and content.
 * FROM: http://bit.ly/lNIlOu
 */
function get_web_page( $url )
{
  $agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30';
  //$agent = 'spider';

  $options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page if successful
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => $agent, // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    CURLOPT_SSL_VERIFYPEER => false,    // Disabled SSL Cert checks
    CURLOPT_SSL_VERIFYHOST => false,    // Disable host checks ?
  );

  $ch      = curl_init( $url );
  curl_setopt_array( $ch, $options );
  $content = curl_exec( $ch );
  $err     = curl_errno( $ch );
  $errmsg  = curl_error( $ch );
  $header  = curl_getinfo( $ch );
  curl_close( $ch );

  $header['errno']   = $err;
  $header['errmsg']  = $errmsg;
  $header['content'] = $content;
  return $header;
}

$url = 'https://ws.admin.washington.edu/student/v4/public/section.xml?year=2011&quarter=autumn&curriculum_abbreviation=BIOL&course_number=&id=&search_by=Instructor';
$result = get_web_page($url);

echo '<pre>CURL result:<br/>';
var_dump($result);
echo '</pre>';

倾销 $ch 的精简版:

A slimmed down version of dumping $ch:

array(24) {
  ["url"]=>
  string(155) "https://ws.admin.washington.edu/student/v4/public/section.xml?year=2011&quarter=autumn&curriculum_abbreviation=BIOL&course_number=&id=&search_by=Instructor"
  ["content_type"]=>
  NULL
  ["http_code"]=>
  int(0)
  ["header_size"]=>
  int(0)
  ["request_size"]=>
  int(237)
  ...
  ["ssl_verify_result"]=>
  int(20)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(120.41427)
  ...
  ["connect_time"]=>
  float(0.11626)
  ...
  ["certinfo"]=>
  array(0) {
  }
  ["errno"]=>
  int(28)
  ["errmsg"]=>
  string(67) "Operation timed out after 120000 milliseconds with 0 bytes received"
  ["content"]=>
  bool(false)
}

当我自己访问该网站时,它只会加载.我什至将代理签名设置为与我自己的完全相同.

When I visit the site myself it simply loads. I even set the agent signature to be the exact same as my own.

任何帮助将不胜感激.

推荐答案

对于我来说添加 curl_setopt($ch, CURLOPT_SSLVERSION, 3); 神奇地解决了这个问题!

For me adding curl_setopt($ch, CURLOPT_SSLVERSION, 3); magically solved the issue!

这篇关于使用旧证书调用 HTTPS 页面时的 cURL 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17