simplexml_load_file通过url读取远程xml,对等连接重置

时间:2018-08-16 09:13:17

标签: php https cron simplexml

当我尝试从下面的URL读取xml输出时,在godaddy共享主机中执行了cron作业。

https://www.bluedart.com/servlet/RoutingServlet?handler=tnt&action=custawbquery&loginid=MAA00001&format=xml&lickey=a28f0bb8690c75ce3368bb1c76ea98bc&verno=1.3&scan=1&awb=awb&numbers=14539611450

我遇到以下错误

  

警告:simplexml_load_file()[function.simplexml-load-file]:SSL:对等连接重置连接   /Applications/XAMPP/xamppfiles/htdocs/indiagsl/cron/test.php在线   32

     

警告:simplexml_load_file()[function.simplexml-load-file]:失败   启用加密   /Applications/XAMPP/xamppfiles/htdocs/indiagsl/cron/test.php在线   32

     

警告:   simplexml_load_file(https://www.bluedart.com/servlet/RoutingServlet?handler=tnt&action=custawbquery&loginid=MAA01849&awb=awb&format=xml&lickey=5eeb55bdce11d065649a32f7e6f6463c&verno=1.3&scan=1&numbers=50545219152)   [function.simplexml-load-file]:无法打开流:操作   失败

我的代码如下

$url = "https://www.bluedart.com/servlet/RoutingServlet?handler=tnt&action=custawbquery&loginid=MAA00001&format=xml&lickey=a28f0bb8690c75ce3368bb1c76ea98bc&verno=1.3&scan=1&awb=awb&numbers=14539611450";
        try {
            echo $i."-";
            $xml = simplexml_load_file($url);
            if(false === $xml) {
                echo "Failed Loading XML";
                foreach(libxml_get_errors() as $errors) {
                    echo "\t", $errors->message ."##";
                }
                $updateFlag=0;
            }
          } catch(Exception $e) {
            echo "ERROR::";
            print_r($e);
        }

请为我提供宝贵的帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

使用db.col.aggregate([ { "$unwind": "$error" }, { "$group": { "_id": null, "distErrKey": { "$addToSet": "$error.errorkey" }, "data": { "$push": "$$ROOT" } } }, { "$unwind": "$data" }, { "$group": { "_id": { "errorkeyname": "$data.error.errorkey" }, "count": { "$sum": 1 }, "distErrKeys": { "$first": "$distErrKey" } } }, { "$sort": { "count": -1 } }, { "$limit": 1 } ]) 添加一个简单的函数来检索文档,并将curl更改为simple_xml_load_file应该可以解决问题。

simple_xml_load_string

取消注释libxml_use_internal_errors(true); //to get the errors from libxml_get_errors() $url = "https://www.bluedart.com/servlet/RoutingServlet?handler=tnt&action=custawbquery&loginid=MAA00001&format=xml&lickey=a28f0bb8690c75ce3368bb1c76ea98bc&verno=1.3&scan=1&awb=awb&numbers=14539611450"; $i = 2; //to get rid of notice error. remove this line function getXml($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //curl_setopt($ch, CURLOPT_CAINFO, "/path/to/cacert.pem"); //absolute path to certificate //curl_setopt($ch, CURLOPT_CAINFO, "c:/path/to/cacert.pem"); $curl_response = curl_exec($ch); curl_close($ch); return $curl_response; } try { echo $i."-"; $xml_file = getXml($url); $xml = simplexml_load_string($xml_file); if(false === $xml) { echo "Failed Loading XML"; foreach(libxml_get_errors() as $errors) { echo "\t", $errors->message ."##"; } $updateFlag=0; } // else die(print_r($xml)); } catch(Exception $e) { echo "ERROR::"; print_r($e); } 的内容如下

else die(print_r($xml));
  

注意:此功能用于回复。在现实世界中,我强烈建议您使用Guzzle

cacert.pem from curl.haxx.se