简体   繁体   English

将 sh+perl 脚本转换为 php 脚本

[英]Convert sh+perl script to php script

I've found a coded to retrieve CA certificates.我找到了一个代码来检索 CA 证书。 The problem is that the script is .sh format:问题是脚本是 .sh 格式:

wget -O - https://eidas.agid.gov.it/TL/TSL-IT.xml | perl -ne 'if (/<X509Certificate>/) {
s/^\s+//; s/\s+$//;
s/<\/*X509Certificate>//g;
print "-----BEGIN CERTIFICATE-----\n";
while (length($_)>64) {
print substr($_,0,64)."\n";
$_=substr($_,64);
}
print $_."\n";
print "-----END CERTIFICATE-----\n";
}' >CA.pem

I'd like to have it on PHP but i don't know how it work in .sh (i use windows and I can't view the output in .sh) so i don't know how to start conversion :(我想在 PHP 上使用它,但我不知道它在 .sh 中是如何工作的(我使用的是 Windows 并且我无法在 .sh 中查看输出)所以我不知道如何开始转换:(

UPDATE: after some try i'm arrived to this point:更新:经过一些尝试,我到达了这一点:

$xmlDoc = simplexml_load_file('https://eidas.agid.gov.it/TL/TSL-IT.xml');

$content = "";

foreach ($xmlDoc->SchemeInformation->PointersToOtherTSL->OtherTSLPointer->ServiceDigitalIdentities->ServiceDigitalIdentity as $key)
{
    $content .= "-----BEGIN CERTIFICATE-----" . PHP_EOL;
    $content .= wordwrap($key->DigitalId->X509Certificate, 64, PHP_EOL, true);
    $content .= PHP_EOL;
    $content .= "-----END CERTIFICATE-----" . PHP_EOL;

}

file_put_contents("test.txt",$content);

The problem is that "X509Certificate" is in other blocks inside the XML, so i've to create a loop for every block.问题是“X509Certificate”在 XML 内的其他块中,所以我必须为每个块创建一个循环。 Is there a way to extract value only by element name = "X509Certificate" without know the correct tree gerarchy?有没有办法只通过元素名称 = "X509Certificate" 提取值而不知道正确的树结构?

Use the xpath() method with a XPath for the node you are looking for:xpath()方法与您要查找的节点的XPath 结合使用:

EDIT: just to be on the safe side I added namespace handling编辑:为了安全起见,我添加了命名空间处理

# default namespace for elements without namespace prefix
$xmlDoc->registerXPathNamespace('default', 'http://uri.etsi.org/02231/v2#');

foreach ($xmlDoc->xpath("//default:X509Certificate") as $element) {

If you stringify $element you should get the text content of the node, ie the Base64 string you are looking for.如果您将$element字符串化,您应该获得节点的文本内容,即您要查找的 Base64 字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM