simplexml_load_file to read remote xml via url, connection reset by peer

若如初见. 提交于 2020-05-16 02:23:11

问题


When i tried to read a the xml output from the below url,over a cron job in godaddy shared hosting.

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 get the following error

Warning: simplexml_load_file() [function.simplexml-load-file]: SSL: Connection reset by peer in /Applications/XAMPP/xamppfiles/htdocs/indiagsl/cron/test.php on line 32

Warning: simplexml_load_file() [function.simplexml-load-file]: Failed to enable crypto in /Applications/XAMPP/xamppfiles/htdocs/indiagsl/cron/test.php on line 32

Warning: 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]: failed to open stream: operation failed

My code is as follows

$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);
        }

Kindly help me with your valuable inputs. Thanks


回答1:


Adding a simple function using curl to retrieve the document, and changing simple_xml_load_file to simple_xml_load_string should do the trick.

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);
 }

uncommenting else die(print_r($xml)); gives below

SimpleXMLElement Object
(
    [Shipment] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [WaybillNo] => 14539611450
                )
            [Status] => Incorrect Waybill number or No Information
            [StatusType] => NF
        )

)

Note This function is for reply purposes. In real world scenarios, I strongly advice you to use Guzzle

cacert.pem from curl.haxx.se



来源:https://stackoverflow.com/questions/51873495/simplexml-load-file-to-read-remote-xml-via-url-connection-reset-by-peer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!