我们有来自 SOAP 的输出 XML,我们需要将其下载到与我们在 XML 中找到的文件名相同的文件夹中。我将 IMG hanno 文件归档为访问密钥:
<?xml version="1.0" encoding="UTF-8"?>
<TableResult>
<Product>
<FDI_0843>https://ws.farmadati.it/WS_DOC/GetDoc.aspx?accesskey=xxxxxxx&tipodoc=TE009&nomefile=001536.jpg</FDI_0843>
<FDI_0840>908872245</FDI_0840>
</Product>
<Product>
<FDI_0843>https://ws.farmadati.it/WS_DOC/GetDoc.aspx?accesskey=xxxxxxx&tipodoc=TE009&nomefile=005075.jpg</FDI_0843>
<FDI_0840>908057906</FDI_0840>
</Product>
我们如何创建脚本来在 Woocommerce 或 FTP 的文件夹中下载这些图像? 谢谢各位热心回复的人
由于 XML 文件中的 URL 受到保护,因此无法完全测试以下内容,但您可以尝试这样,请注意 XML 已被稍微修改
$xmlstring='<?xml version="1.0" encoding="UTF-8"?>
<TableResult>
<Product>
<FDI_0843>https://ws.farmadati.it/WS_DOC/GetDoc.aspx?accesskey=0123BFJ-652e56b0beCad9CBe1c375daCa9A089D35Aab280c&tipodoc=TE009T&nomefile=001536.jpg</FDI_0843>
<FDI_0840>908872245</FDI_0840>
</Product>
<Product>
<FDI_0843>https://ws.farmadati.it/WS_DOC/GetDoc.aspx?accesskey=0223BFJ-652e56b0beCad9CBe1c375daCa9A089D35Aab280c&tipodoc=TE009X&nomefile=005075.jpg</FDI_0843>
<FDI_0840>908057906</FDI_0840>
</Product>
</TableResult>';
/**************************************************
Determine where you will save files to
and amend this path as necessary. Currently
this creates a new directory under the current
working directory if it does not exist.
*/
$save_directory=__DIR__ . '\\save_images_here';
if( !file_exists( $save_directory ) ) {
mkdir( $save_directory, 0777, true );
}
/*******************************************
Create the DOMDocument instance & load
the XML string ( or file )
Query the DOM to find all relevant nodes
and process that nodelist to extract
name of file.
Note in the xml string the ampersand
is encoded as HTML entity!
*/
libxml_use_internal_errors( true );
$dom = new DOMDocument;
$dom->validateOnParse=false;
$dom->strictErrorChecking=false;
$dom->recover=true;
$dom->loadXML( $xmlstring );
libxml_clear_errors();
/*
Find all nodes - FDI_0843
*/
$col=$dom->getElementsByTagName('FDI_0843');
if( $col && $col->length > 0 ){
foreach( $col as $node ){
# decode the value held by the node
$url=urldecode( $node->nodeValue );
# extract the querystring
$querystring=parse_url( $url, PHP_URL_QUERY );
# parse the querystring
parse_str( $querystring, $output );
$nomefile=$output['nomefile'];
# create new filepath, download and save target file
$filepath=sprintf('%s\\%s',$save_directory,$nomefile);
$filedata=file_get_contents( $url );
file_put_contents( $filepath, $filedata );# error here ~ had arguments back to front
#... next
}
}