什么是重复的Curl选项,为什么它们的常量名称不同? [英] What are duplicate Curl Options and why do their constant names differ?

查看:85
本文介绍了什么是重复的Curl选项,为什么它们的常量名称不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP curl库中,可以使用 curl_setopt() 函数,例如

  $ curl = curl_init($ url);curl_setopt($ curl,CURLOPT_FOLLOWLOCATION,TRUE); 

允许curl遵循对给定" Location:... "标头的重定向响应.常量 CURLOPT_FOLLOWLOCATION 表示表示curl库中选项的整数,这里示例整数值为 CURLOPT_FOLLOWLOCATION int(52).

当通过选项整数值获取常量名称时,由于某些常量具有相同的值(例如,在解决方案

不同的命名选项常量表示相同的设置或选项.这是由于基础curl库随时间变化而引起的.您不仅可以在PHP中互换使用它们,还可以在curl库中互换使用它们,因为PHP从libcurl c库中导入了这些常量.

这也是我在一开始就可以想象的,其中一些选项只是一个别名.以curl选项 int(10009)为例,它分配了两个不同的命名常量:

  1. CURLOPT_READDATA :未记录的常量
  2. CURLOPT_INFILE ::上传时应从中读取传输文件的流资源.

第一个未记录在PHP手册中.curl库在此处进行了记录: http://curl.haxx.se/libcurl/c/CURLOPT_READDATA.html -它实际上与PHP中的 CURLOPT_INFILE 含义相同.可以在卷曲的选项列表中以及何时使用作为其来源的一部分:

 引入的名称已弃用已删除CURLOPT_READDATA 7.9.7CURLOPT_INFILE 7.1 7.9.7 

还可以在源中检查别名的状态(参考):

  #define CURLOPT_INFILE CURLOPT_READDATA/*名称在7.9.7中已更改*/ 

CURLOPT_INFILE 显然是 CURLOPT_READDATA 的别名,因为它已被弃用.PHP手册尚未反映出这一点.

还有另一组不同的命名选项常量代表相同的选项:

  1. CURLOPT_KEYPASSWD ::使用 CURLOPT_SSLKEY CURLOPT_SSH_PRIVATE_KEYFILE 私钥所需的密码字符串.在cURL 7.16.1中添加.
  2. CURLOPT_SSLCERTPASSWD ::使用 CURLOPT_SSLCERT 证书所需的密码字符串.
  3. CURLOPT_SSLKEYPASSWD ::使用在 CURLOPT_SSLKEY 中指定的私人SSL密钥所需的秘密密码的字符串.

再次再次进行版本检查:

 引入的名称已弃用已删除CURLOPT_KEYPASSWD 7.17.0CURLOPT_SSLCERTPASSWD 7.1.1 7.17.0CURLOPT_SSLKEYPASSWD 7.9.3 7.17.0 

这已经表明不推荐使用后两个.让我们看一下常量定义( ref &参考):

  .../*这些计划于2009年消失*//*在7.17.0中添加了以下内容*/#define CURLOPT_SSLKEYPASSWD CURLOPT_KEYPASSWD...#define CURLOPT_SSLCERTPASSWD CURLOPT_KEYPASSWD... 

因此可以清楚地说, CURLOPT_SSLKEYPASSWD CURLOPT_SSLCERTPASSWD 都是 CURLOPT_KEYPASSWD 的别名.

因为这将以我知道的PHP curl扩展名和curl_setopt() function, for example

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);

allows curl to follow redirect responses to the given "Location: ..." headers. The constant CURLOPT_FOLLOWLOCATION represents an integer denoting the option in the curl library, here the example integer value is int(52) for CURLOPT_FOLLOWLOCATION.

When obtaining the constant name by the option integer value it is not possible to exactly find out which option constant was used because some constants have the same value (for example shown in Get a constant name as a string).

This leads to the question: Why are there duplicate constant names for the same options and what does this mean?

解决方案

The different named option constants are representing the same settings or options. This is due to changes in the underlying curl library over time. You can not only interchangeably use them in PHP but also in the curl library because PHP imports those constants from the libcurl c library.

This is also what I could imagine in the beginning that some of those options are just an alias. Take for example the curl option int(10009), it has two different named constants assigned:

  1. CURLOPT_READDATA: undocumented constant
  2. CURLOPT_INFILE: Stream resource of the file that the transfer should be read from when uploading.

The first one is undocumented in the PHP manual. The curl library has it documented here: http://curl.haxx.se/libcurl/c/CURLOPT_READDATA.html - It effectively has the same meaning as CURLOPT_INFILE in PHP. This can be easily tracked in the curl list of options and when introduced as part of their source:

 Name                           Introduced  Deprecated  Removed

CURLOPT_READDATA                7.9.7
CURLOPT_INFILE                  7.1           7.9.7

Also the status of an alias can be checked as well in the sources (ref):

#define CURLOPT_INFILE CURLOPT_READDATA /* name changed in 7.9.7 */

CURLOPT_INFILE is clearly an alias of CURLOPT_READDATA because it has been deprecated. The PHP manual does not yet reflect this.

There is another group of different named option constants which are standing for the same option:

  1. CURLOPT_KEYPASSWD: String of the password required to use the CURLOPT_SSLKEY or CURLOPT_SSH_PRIVATE_KEYFILE private key. Added in cURL 7.16.1.
  2. CURLOPT_SSLCERTPASSWD: String of the password required to use the CURLOPT_SSLCERT certificate.
  3. CURLOPT_SSLKEYPASSWD: String of the secret password needed to use the private SSL key specified in CURLOPT_SSLKEY.

First again the version check:

 Name                           Introduced  Deprecated  Removed

CURLOPT_KEYPASSWD               7.17.0
CURLOPT_SSLCERTPASSWD           7.1.1         7.17.0
CURLOPT_SSLKEYPASSWD            7.9.3         7.17.0

This shows already that the later two are deprecated. Let's see the constant definitions (ref & ref):

...
/* These are scheduled to disappear by 2009 */

/* The following were added in 7.17.0 */
#define CURLOPT_SSLKEYPASSWD CURLOPT_KEYPASSWD
...
#define CURLOPT_SSLCERTPASSWD CURLOPT_KEYPASSWD
...

So it can be clearly said that both CURLOPT_SSLKEYPASSWD and CURLOPT_SSLCERTPASSWD are an alias of CURLOPT_KEYPASSWD.

As this reviews all the collisions in the constant names I'm aware of for the PHP curl extension and the PHP extension imports the constants from the libcurl C library, you can be assured that those are aliases.

It's perhaps most safe to use the non-deprecated constants in your code for future compatibility:

  • CURLOPT_READDATA (since 7.9.7)
  • CURLOPT_KEYPASSWD (since 7.17.0)

This would be in opposite to:

  • CURLOPT_INFILE (since 7.1, now deprecated since 7.9.7)
  • CURLOPT_SSLCERTPASSWD (since 7.1.1, now deprecated since 7.17.0)

这篇关于什么是重复的Curl选项,为什么它们的常量名称不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆