【发布时间】:2013-07-19 00:00:34
【问题描述】:
我一直在研究和试验这个问题,但还没有找到可行的解决方案,所以我认为是时候寻求帮助了。
curl_exec 有问题,但仅限于特定服务器。首先是一些背景:
- CPU:英特尔酷睿 I7
- 内存:64GB
- 操作系统:Windows 8.0
- 服务器:Apache 2.4.4 x86 TS
- PHP 版本:5.5.1 x86 TS w/xDebug 2.2.3
- cURL 版本:7.30.0
出现问题的 PHP 代码:
$input_vars = (!empty($_POST)) ? filter_input_array(INPUT_POST) : array();
$url = 'http://192.168.1.100/geekcavecreations/Morti/chatbot/conversation_start.php';
$qs = '?';
foreach ($input_vars as $key => $value)
{
$qs .= "$key=$value&";
}
$qs= rtrim($qs, '&');
$url .= $qs;
$bot_id = $input_vars['bot_id'];
$options = array(
CURLOPT_USERAGENT => 'Program O XML API',
CURLOPT_RETURNTRANSFER => true,
//CURLOPT_POST => 1,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $input_vars);
$data = curl_exec($ch);
$debug = curl_getinfo($ch);
curl_close($ch);
echo '<pre>Data = ', htmlentities($data), '</pre><br>';
var_dump($debug);
可以看出,我用 GET 和 POST 都试过了,都给出了相同的结果。上面列出的超时选项是为了让脚本不会无限期地运行。在我停止 Apache 服务以停止挂断之前,我已经挂了 3 个多小时(只是在浏览器中取消不会这样做)。脚本的输出如下:
array (size=26)
'url' => string 'ht tp://192.168.1.100/geekcavecreations/Morti/chatbot/conversation_start.php?say=hello&bot_id=1&convo_id=78a9s39gut34lurq055in5s6r4&format=xml' (length=141)
'content_type' => null
'http_code' => int 0
'header_size' => int 0
'request_size' => int 203
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 5
'namelookup_time' => float 0
'connect_time' => float 0
'pretransfer_time' => float 0
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float 0
'starttransfer_time' => float 0
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string '192.168.1.100' (length=13)
'certinfo' =>
array (size=0)
empty
'primary_port' => int 80
'local_ip' => string '192.168.1.100' (length=13)
'local_port' => int 2546
Data =
(找不到更好的格式化方式,抱歉)
同一台计算机上还有几台虚拟机,每台都有不同的操作系统/服务器/PHP版本,并且都具有完全相同的物理文档根目录,位于主机上。这些机器从 Windows 7/IIS 到 CentOS/Apache 2.2 以及其他组合,无一例外地运行相同的脚本,并输出预期的 XML 文档。如果我只在 Web 浏览器中运行 URL,则输出如下:
<?xml version="1.0"?>
<program_o>
<version>2.3.0</version>
<status><success>1</success></status>
<bot_id>1</bot_id>
<bot_name>Morti</bot_name>
<user_id>1</user_id>
<user_name>Seeker</user_name>
<chat>
<line>
<input>hello</input>
<response>And a good failed to you, undefined. How are you?</response>
</line>
</chat>
</program_o>
我还获取了上述 XML 输出并将其保存到一个文件中,并让问题脚本对该保存的 XML 文件的 URL 执行 cURL 调用,并且该脚本此时可以正常工作,所以我还创建了一个模型脚本,它只创建一个 SimpleXMLElement 对象,填充一些新标签,然后从创建的对象中回显 asXML() 输出(基本上是 conversation_start.php 所做的,但远没有那么复杂),我得到了同样的问题。样机脚本的代码如下:
$xml = new SimpleXMLElement('<program_o></program_o>');
$xml->addChild('version', '2.3.0');
$status = $xml->addChild('status');
$status->addChild('success', '1');
$xml->addChild('bot_id', '1');
$xml->addChild('bot_name', 'Morti');
$xml->addChild('user_id', '1');
$xml->addChild('user_name', 'Seeker');
$chat = $xml->addChild('chat');
$line = $chat->addChild('line');
$line->addChild('input', 'hello');
$line->addChild('response', 'And a good failed to you, undefined. How are you?');
$output = $xml->asXML();
header('Content-type: text/xml');
exit($output);
到此为止,我已经不知所措了。我已经更改了 PHP 版本、Apache 版本,尝试了我在 SO 上找到的无数关于 cURL 冻结的其他问题的建议,如发现 here、here 和 here 以及其他几十个或更多。
现在我已经写了一本书来介绍我的问题,我不得不问:我怎样才能让 cURL 避免挂在 Windows 8 平台上?
【问题讨论】: