PHP cURL XML示例有时返回空白,大多数时候



下面是我的代码示例:

function xmlPostStore($post_xml, $url, $port) {
    $ch = curl_init(); // initialize curl handle
    curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
    curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
    curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml); // add POST fields
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

我发送的内容:

$XML = "<?xml version="1.0" encoding="utf-8"?>
        <SendConfig>
            <Request>
                <key>passphrase</key>
            </Request>
        </SendConfig>";
$reply = xmlPostStore($XML, "http://www.urlhere.com/test.php", "80");

test.php只是一个简单的XML返回:

echo "<?xml version='1.0' encoding='utf-8'?>
<response>
    <config>
        <test>it works</test>
    </config>
</response>";

当我在一台服务器上测试它时,它100%有效。我收到回复,没有任何问题。

当我在主服务器上测试它时,它没有返回任何东西,大多数时候,大约98%的时间它是空白的。没有任何代码更改,它将随机工作和随机停止。我一头雾水。

原来我以为打开防火墙允许传入和传出的连接只允许传入,不允许传出。我还解决了resolv.conf下的DNS错误,现在工作正常。

最新更新