simplexml_load_file通过url读取远程xml,对等体重置连接 [英] simplexml_load_file to read remote xml via url, connection reset by peer
问题描述
当我尝试从下面的URL读取xml输出时,在godaddy共享主机中执行了cron作业。
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
我得到以下错误
警告:simplexml_load_file()[function.simplexml-load-file]:SSL:连接重置
/Applications/XAMPP/xamppfiles/htdocs/indiagsl/cron/test.php中的同行
32
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
警告:simplexml_load_file()[ function.simplexml-load-file]:
无法在
/Applications/XAMPP/xamppfiles/htdocs/indiagsl/cron/test.php中启用加密功能,在
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
警告:
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]:无法打开流:操作
失败
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
我的代码如下
$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
推荐答案
使用 curl
向中添加一个简单函数检索文档,然后将 simple_xml_load_file
更改为 simple_xml_load_string
应该可以解决问题。
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);
}
取消注释否则die(print_r($ xml)) ;
在下面给出
SimpleXMLElement Object
(
[Shipment] => SimpleXMLElement Object
(
[@attributes] => Array
(
[WaybillNo] => 14539611450
)
[Status] => Incorrect Waybill number or No Information
[StatusType] => NF
)
)
注意此功能用于答复。在现实世界中,我强烈建议您使用 Guzzle
Note This function is for reply purposes. In real world scenarios, I strongly advice you to use Guzzle
这篇关于simplexml_load_file通过url读取远程xml,对等体重置连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!