Search code examples
phpxmlwoocommercesoapwsdl

Extrapolate and download from xml images asp link in WooCommerce


we have output XMLs from SOAP, which we need to download to a folder with the same filename that we find in the XML. I file IMG hanno un accesskey:

<?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>

How can we create a script to download these images in Woocommerce or in a folder in our FTP? Thank you for those who will respond


Solution

  • As the URLS within the XML file are protected the following cannot be tested fully but you can try like this, note that the XML as been modified slightly

    $xmlstring='<?xml version="1.0" encoding="UTF-8"?>
    <TableResult>
        <Product>
            <FDI_0843>https://ws.farmadati.it/WS_DOC/GetDoc.aspx?accesskey=0123BFJ-652e56b0beCad9CBe1c375daCa9A089D35Aab280c&amp;tipodoc=TE009T&amp;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&amp;tipodoc=TE009X&amp;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
        }
    }