我无法加载xml文件.
这段代码效果很好:
$url = 'http://www.w3schools.com/xml/note.xml';
$xml = simplexml_load_file($url);
print_r($xml);
Run Code Online (Sandbox Code Playgroud)
但这一个
$url = 'https://www.boardgamegeek.com/xmlapi2/thing?id=105551';
$xml = simplexml_load_file($url);
print_r($xml);
Run Code Online (Sandbox Code Playgroud)
不起作用.我收到此错误:
警告:simplexml_load_file():SSL操作失败,代码为1. OpenSSL错误消息:错误:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:证书验证在/storage/content/59/113059/boardgamelevelup.com/public_html/index.php上失败19警告:simplexml_load_file():无法在第19行的/storage/content/59/113059/boardgamelevelup.com/public_html/index.php中启用加密警告:simplexml_load_file(https://www.boardgamegeek.com/xmlapi2/thing ?id = 105551):无法打开流:第19行/storage/content/59/113059/boardgamelevelup.com/public_html/index.php中的操作失败警告:simplexml_load_file():I/O警告:无法加载外部实体" https://www.boardgamegeek.com/xmlapi2/thing?id=105551"在第19行的/storage/content/59/113059/boardgamelevelup.com/public_html/index.php中
boardgamegeek的xml文件适用于其他网站.我应该使用不同的PHP代码来加载该xml文件吗?
简短的食谱答案:
加
$context = stream_context_create(array('ssl'=>array(
'verify_peer' => true,
'cafile' => '/path/to/ca-bundle.crt'
)));
libxml_set_streams_context($context);到你的脚本,所以它在simplexml_load_file()之前执行.
或者 - 而不是上面的代码 - openssl.cafile=/path/to/ca-bundle.crt在php.ini中设置.
非常简短的解释:
你的php版本使用openssl来处理http 的传输.openssl试图验证服务器是否真的是它声称的那个.它通过检查其证书是否可信来实现.X.509证书包含有关所有者的一些数据,并由颁发者签署(本身具有再次签名的证书,依此类推,直到所有者和发行者相同的证书 - >自签名/根证书).证书被认为是"可信的",如果在该证书链中有(至少)一个证书,openssl"说":"好的,我已被指示信任这个证书".该指令采用(或可以)的形式采取的形式)"这里是一个包含你应该信任的证书的文件"(cafile).
上面的代码告诉php的libxml-wrapper告诉openssl当simplexml_load_file使用https/openssl-wrapper时那个cafile.
并将其openssl.cafile=/path/to/ca-bundle.crt设置为默认值; 除非另有说明,否则所有openssl操作都将使用该文件 - 包括libxml/simple_xml_loadfile.
我链接的ca-bundle.crt来自一个"声称"提供mozilla firefox附带的提取的根证书的项目.关于"索赔":我没有理由怀疑这确实是未被禁止的根证书清单; 但你永远不会知道:你在这个项目中信任a)b)mozilla做得很好,只把可靠的证书放在那个清单中......
@VolkerK 展示的工作和示例非常出色且简单。虽然这种方法对我不起作用,但我更进一步,暂时基本上取消了安全性。
$context = stream_context_create(array('ssl'=>array(
'verify_peer' => false,
"verify_peer_name"=>false
)));
libxml_set_streams_context($context);
$sxml = simplexml_load_file($webhostedXMLfile);
Run Code Online (Sandbox Code Playgroud)
是的,这是不好的做法,但有时您需要临时修复而不是这样的消息:
警告:simplexml_load_file():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:/srv/www/resources/public_html/files/etc/file.php 第 150 行中的证书验证失败警告: simplexml_load_file(): 无法在第 150 行的 /srv/www/resources/public_html/files/etc/file.php 中启用加密
我希望它可以帮助别人。