使用cURL进行的SSL请求在进程fork之后失败 [英] SSL Requests made with cURL fail after process fork
问题描述
我遇到了一些很奇怪的行为curl
I have run into some pretty strange behaviour with curl
- 如果我在父进程中使用curl进行SSL请求,然后
fork过程,并尝试在子
进程中进行另一个SSL请求,尝试失败,错误no。 35(SSL连接错误)。 - 如果我不在父级中发出SSL请求,则子进程中的SSL请求成功。
- 我可以在父级和子级中的SSL请求中创建任意数量的非SSL请求。
这似乎是libcurl中的一个错误相关问题和回答者拥有解决问题。
It appears that this is a bug in libcurl related question and the answerer has a work around for it.
我的问题是:
- curl_global_cleanup (由PHP API中的其他名称公开)?
- 如果没有,还有其他工作吗?
- Is curl_global_cleanup exposed by some other name in the PHP API?
- If not is there some other work around?
$ch = curl_init('https://www.google.ca/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$success = curl_exec($ch);
var_dump($success !== false); // true
curl_close($ch);
$pid = pcntl_fork();
if ($pid === 0) {
$ch = curl_init('http://www.google.ca/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$success = curl_exec($ch);
var_dump($success !== false); // true
curl_close($ch);
$ch = curl_init('https://www.google.ca/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$success = curl_exec($ch);
var_dump($success !== false); // false
$errno = curl_errno($ch); // 35
$error = curl_error($ch); // SSL connect error
curl_close($ch);
} else if ($pid > 0) {
// wait for child process
pcntl_wait($status);
} else {
// handel fork error
}
如果这不是libcurl的错误,它是我做错了,请让我知道。
If this is not a bug with libcurl and it is something I am doing wrong please let me know.
推荐答案
通过使用 pcntl_exec 功能。所以你基本上会删除你的子代码,并把它放在一个不同的php文件,然后在子进程中执行(并实际上替换)。
I just solved this by using the pcntl_exec function. So you would essentially remove your child code and place it in a different php file which then gets executed in (and actually replaces) the child process.
$ch = curl_init('https://www.google.ca/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$success = curl_exec($ch);
var_dump($success !== false); // true
curl_close($ch);
$pid = pcntl_fork();
if ($pid === 0) {
pcntl_exec('child_curl.php');
} else if ($pid > 0) {
// wait for child process
pcntl_wait($status);
} else {
// handel fork error
}
child_curl.php的内容:
Contents of child_curl.php:
$ch = curl_init('http://www.google.ca/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$success = curl_exec($ch);
var_dump($success !== false); // true
curl_close($ch);
$ch = curl_init('https://www.google.ca/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$success = curl_exec($ch);
var_dump($success !== false); // false
$errno = curl_errno($ch); // 35
$error = curl_error($ch); // SSL connect error
curl_close($ch);
执行子程序后,您将看不到错误。
After the execution of the child you will not see an error.
这些其他问题帮助了我:
These other issues helped me out:
- libCurl SSL error after fork()
- Curl and pcntl_fork()
这篇关于使用cURL进行的SSL请求在进程fork之后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!