gpt4 book ai didi

php - 如何将 zip/压缩文件转换为 PHP 中的字符串?

转载 作者:行者123 更新时间:2023-12-05 05:27:50 25 4
gpt4 key购买 nike

我正在制作一个 php 脚本来创建/构建 XML 请求,压缩 xml,然后将其编码为 base64。

这是我的代码片段:

$url = "http://cc-dev.com/servlet/Integration";

$xml = "<Transactions>\n";
$xml.= "<Search_Criteria>\n";
$xml.= "<CC_enquiry>Y</CC_enquiry>\n";
$xml.= "<CCPlusCPA_enquiry>N</CCPlusCPA_enquiry>\n";
$xml.= "<NLR_enquiry>N</NLR_enquiry>\n";
$xml.= "<CON_enquiry>N</CON_enquiry>\n";
$xml.= "<Identity_number>{$idnum}</Identity_number>\n";
$xml.= "<Surname>{$lname}</Surname>\n";
$xml.= "<Forename>{$fname}</Forename>\n";
$xml.= "<Forename2></Forename2>\n";
$xml.= "<Forename3></Forename3>\n";
$xml.= "<Gender>{$gender}</Gender>\n";
$xml.= "<Passport_flag>{$passport}</Passport_flag>\n";
$xml.= "<DateOfBirth>{$birthday}</DateOfBirth>\n";
$xml.= "<Address1>{$address1}</Address1>\n";
$xml.= "<Address2></Address2>\n";
$xml.= "<Address3></Address3>\n";
$xml.= "<Address4></Address4>\n";
$xml.= "<PostalCode>{$postalcode}</PostalCode>\n";
$xml.= "<HomeTelCode></HomeTelCode>\n";
$xml.= "<HomeTelNo></HomeTelNo>\n";
$xml.= "<WorkTelCode></WorkTelCode>\n";
$xml.= "<WorkTelNo></WorkTelNo>\n";
$xml.= "<CellTelNo></CellTelNo>\n";
$xml.= "<ResultType>XML</ResultType>\n";
$xml.= "<RunCodix>N</RunCodix>\n";
$xml.= "<Adrs_Mandatory>N</Adrs_Mandatory>\n";
$xml.= "<Enq_Purpose>18</Enq_Purpose>\n";
$xml.= "<Run_CompuScore>N</Run_CompuScore>\n";
$xml.= "</Search_Criteria>\n";
$xml.= "</Transactions>\n";


$ttype = urlencode(base64_encode("Normal_Search"));
$dll = urlencode(base64_encode("Direct01"));
$un = urlencode(base64_encode($username));
$pw = urlencode(base64_encode($password));
$origin = urlencode(base64_encode("TestProgram"));
$trans = urlencode(base64_encode(gzencode($xml)));

header("Location: $url?pTransType=$ttype&pUsrnme=$un&pPasswrd=$pw&pDLL_Version=$dll&pMyOrigin=$origin&pTransaction=$trans");

$trans 的输出应该是这样的:

UEsDBBQAAgAIAFhtdUFbdvjAZwEAAJADAAAWAAAATm9ybWFsU2VhcmNoVjVYTWh0LnhtbGWTa0%2BDMBSGP8/E31IuM5tJ02SibhrHlo1E/UTqOHNEaFkvifx7odwKfjvv%2B7zt4ZBTHAnKJD2plDNJbm9m%2BAhUnC5xIFIFIqW1N8NBEAO76lSU5BMjS7V0n2kZ7Ffj0NQ02fDtYKds2dy1C3sjrG6xpOEvCTCVqjJmOv8CQRZzZ%2Bkt71xn4SwdjKbYnDlqwWgOZM35ucSok4Y9cwFGvWqpMOrlCHpkIN4Y%2BRbyG7QGllSttxi1lXH3VMqCCxWfM/pdjzY2TOaRKtidH1KhLsS9b0bDyHZNbJUkAqR0q9Z9aQNvAN4I%2BAPwR2A%2BgHn7uVwqmgU8gQpZwsANzyGCjtrKxiEfYFUb9M7Fz3DSVjY2J4e6WQzIsg4NtUEHkDpTUVkA%2BdhuIowsowloVvVIf%2Bvf3tft/ELGW8oSqnizcRPHpJ7YNd5rUXAJxPUwsnXXIA54XujjqdqFerEnTv2y0P%2BnhdHk/f0BUEsBAhYLFAACAAgAWG11QVt2%2BMBnAQAAkAMAABYAAAAAAAAAAQAgAICBAAAAAE5vcm1hbFNlYXJjaFY1WE1odC54bWxQSwUGAAAAAAEAAQBEAAAAmwEAAAAA

我试过使用 gzencode()、gzdeflate() 和 gzcompress(),但没有得到我想要的结果。你能帮我实现格式为“UEsDBBQ....AA”的字符串吗?

我认为它与压缩有关,但我似乎无法找到合适的使用方法。提前致谢。

最佳答案

感谢所有评论。我找到了解决问题的办法。 ZipArchive 函数用于压缩 xml。

代码如下:

<?php

header('Content-Type: text/plain');

$s = '<Transactions>
<Search_Criteria>
<CC_enquiry>Y</CC_enquiry>
<CCPlusCPA_enquiry>Y</CCPlusCPA_enquiry>
<NLR_enquiry>Y</NLR_enquiry>
<CON_enquiry>N</CON_enquiry>
<Identity_number>7408285107080</Identity_number>
<Surname>Goofy</Surname>
<Forename>Just</Forename>
<Forename2></Forename2>
<Forename3></Forename3>
<Gender>M</Gender>
<Passport_flag>N</Passport_flag>
<DateOfBirth>19740828</DateOfBirth>
<Address1></Address1>
<Address2></Address2>
<Address3></Address3>
<Address4></Address4>
<PostalCode></PostalCode>
<HomeTelCode></HomeTelCode>
<HomeTelNo></HomeTelNo>
<WorkTelCode></WorkTelCode>
<WorkTelNo></WorkTelNo>
<CellTelNo></CellTelNo>
<ResultType>XMHT</ResultType>
<RunCodix>N</RunCodix>
<Adrs_Mandatory>N</Adrs_Mandatory>
<Enq_Purpose>12</Enq_Purpose>
<Run_CompuScore>Y</Run_CompuScore>
</Search_Criteria>
</Transactions>';

@unlink('s.zip');

$zip = new ZipArchive;
$res = $zip->open('s.zip', ZipArchive::CREATE);

if ($res === true) {

$zip->addFromString('NormalSearchV5XMht.xml', $s);
$zip->close();

$s = file_get_contents('s.zip');
$s = base64_encode($s);
$s = urlencode($s);

echo $s;
// UEsDBBQAAAAIAIO7tEJLe43NZwEAAI4DAAAWAAAATm9ybWFsU2VhcmNoVjVYTWh0LnhtbGWTa0%2BDMBSGP8%2FE31IuM5tJ02SibhrHlo1E%2FUTqOHNEaFkvifx7odwKfjvv%2B7zt4ZBTHAnKJD2plDNJbm9m%2BAhUnC5xIFIFIqW1N8NBEAO76lSU5BMjS7V0n2kZ7Ffj0NQ02fDtYKds2dy1C3sjrG6xpOEvCTCVqjJmOv8CQRZzZ%2Bkt71xn4SwdjKbYnDlqwWgOZM35ucSok4Y9cwFGvWqpMOrlCHpkIN4Y%2BRbyG7QGllSttxi1lXH3VMqCCxWfM%2FpdjzY2TOaRKtidH1KhLsS9b0bDyHZNbJUkAqR0q9Z9aQNvAN4I%2BAPwR2A%2BgHn7uVwqmgU8gQpZwsANzyGCjtrKxiEfYFUb9M7Fz3DSVjY2J4e6WQzIsg4NtUEHkDpTUVkA%2BdhuIowsowloVvVIf%2Bvf3tft%2FELGW8oSqnizcRPHpJ7YNd5rUXAJxPUwsnXXIA54XujjqdqFerEnTv2y0P%2BnhdHo%2Ff0BUEsBAgAAFAAAAAgAg7u0Qkt7jc1nAQAAjgMAABYAAAAAAAAAAAAAAAAAAAAAAE5vcm1hbFNlYXJjaFY1WE1odC54bWxQSwUGAAAAAAEAAQBEAAAAmwEAAAAA

}
else {
echo 'failed';
}

@unlink('s.zip');

?>

感谢 Ramon Alivio 先生对我的帮助。

关于php - 如何将 zip/压缩文件转换为 PHP 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16646109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com