How to upload a chunk xml file with php?

348 views
Skip to first unread message

Leandro Salar

unread,
Sep 11, 2016, 2:59:47 PM9/11/16
to DevTargetProcess
Hi!, 
i can upload files without problem if his MB are less of 3MB, but if i try upload a file with 8MB the server response is an error.

How can i chunk this xml data with php?


this is the code.
 ......

    $ch     = curl_init(); // Inicia la conexion cURL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

    $input_xml='<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:tp="http://targetprocess.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><soap:Header><wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:Username xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">system</wsse:Username><wsse:Password xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">xxxxxx</wsse:Password></wsse:UsernameToken></wsse:Security></soap:Header><soap:Body><AppendChunk xmlns="http://targetprocess.com"><tp:fileName xsi:type="xsd:string">HD'.$id_request.'_'.$name.'</tp:fileName><tp:buffer xsi:type="xsd:base64Binary">'.$file_buffer.'</tp:buffer><tp:Offset xsi:type="xsd:integer">0</tp:Offset><tp:BytesRead xsi:type="xsd:integer">'.$size.'</tp:BytesRead></AppendChunk></soap:Body></soap:Envelope>';

    curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);


    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $headers =  array('Host: xxxxx',
        'Content-Type: text/xml; charset=utf-8',
        'Content-length: '.strlen($input_xml).'',
        'SOAPAction: http://targetprocess.com/AppendChunk'
    ); // Arma el header para validar al usuario
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Envia la validacion

    .....

tsar...@targetprocess.com

unread,
Sep 12, 2016, 11:28:08 AM9/12/16
to DevTargetProcess
Hello!

That's quite hard to do it PHP with raw CURL requests. That's better to use SOAP client (http://php.net/manual/en/class.soapclient.php). 
The main flow is:
  • Read attachment by chunks.
  • Send chunks one by one to a SOAP endpoint,
  • Add attachment to a request. 
We have an excellent and simple sample how to do it in .NET,
so if you want, you can see it there: https://github.com/TargetProcess/RestAPISamples/tree/master/Tp.UploadAttachment

Hope, this helps.

Leandro Salar

unread,
Sep 22, 2016, 5:08:47 PM9/22/16
to DevTargetProcess
Sorry i'm with PHP,
when i send the chunked file, only upload the final push.

has anybody an example to share with me please?.

My xml Examples:

Work fine:

<?xml version="1.0" encoding="UTF-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Header><wsse:Security soap12:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:Username xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">xxxxxxxx</wsse:Username><wsse:Password xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">xxxxxxxxx</wsse:Password></wsse:UsernameToken></wsse:Security></soap12:Header><soap12:Body><AppendChunk xmlns="http://targetprocess.com"><fileName>IMAGEN_1.png</fileName><buffer >iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAS9JREFUeNpifPD2FgMSYALicCCOBGILIBYF4tdAfAKIlwPxSiD+B1PMiKRZCYhXAbExA25wFojDgPgezCaYxuMENDJA5Y9D1YM1M0GdI8ZAHBCDqmcCaYwAYhMG0gBIfQRIcyy6zNYHaxns99kx1JwpZvj65ysuA2KZsNnqrRDM0KRRy3D081mG2rPFDB9/vsemWR+kWQSbjL2UK0OKdDTD8a9XGCZd7cKmRBik+Q02mYPPdjPMebqUwYxbhyFPuwybkrcgzWfQRbc9XMdQd6OZwZrXiKHNuJeBn10Qm+aLoEQSBWQsZSAdRINsXoHNdgIApH4FEzStguL6FZEaX0HV/4Mlz7tAbAVNuwwE0rYVVD08bcMMMANiUBhsBuJnQPwLiJ9D+dFQ+bswDQABBgCT4FBPo1peFAAAAABJRU5ErkJggg==</buffer><Offset>0</Offset><BytesRead>397</BytesRead></AppendChunk></soap12:Body></soap12:Envelope>



Chunk files:

1.
<?xml version="1.0" encoding="UTF-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
..header...
<soap12:Body><AppendChunk xmlns="http://targetprocess.com"><fileName>IMAGEN_1.png</fileName><buffer>iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAS9JREFUeNp</buffer><Offset>0</Offset><BytesRead>80</BytesRead></AppendChunk></soap12:Body></soap12:Envelope>

2.
...
<soap12:Body><AppendChunk xmlns="http://targetprocess.com"><fileName>IMAGEN_1.png</fileName><buffer>ifPD2FgMSYALicCCOBGILIBYF4tdAfAKIlwPxSiD+B1PMiKRZCYhXAbExA25wFojDgPgezCaYxuMENDJA5Y9D1YM1M0GdI8ZAHBCDqmcCaY</buffer><Offset>80</Offset><BytesRead>80</BytesRead></AppendChunk></soap12:Body></soap12:Envelope>

3.
...
<soap12:Body><AppendChunk xmlns="http://targetprocess.com"><fileName>IMAGEN_1.png</fileName><buffer>wAYhMG0gBIfQRIcyy6zNYHaxns99kx1JwpZvj65ysuA2KZsNnqrRDM0KRRy3D081mG2rPFDB9/vsemWR+kWQSbjL2UK0OKdDTD8a9XGCZd7</buffer><Offset>160</Offset><BytesRead>80</BytesRead></AppendChunk></soap12:Body></soap12:Envelope>

4.
...
<soap12:Body><AppendChunk xmlns="http://targetprocess.com"><fileName>IMAGEN_1.png</fileName><buffer>cKmRBik+Q02mYPPdjPMebqUwYxbhyFPuwybkrcgzWfQRbc9XMdQd6OZwZrXiKHNuJeBn10Qm+aLoEQSBWQsZSAdRINsXoHNdgIApH4FEzSt</buffer><Offset>240</Offset><BytesRead>80</BytesRead></AppendChunk></soap12:Body></soap12:Envelope>

5.
...
<soap12:Body><AppendChunk xmlns="http://targetprocess.com"><fileName>IMAGEN_1.png</fileName><buffer>guL6FZEaX0HV/4Mlz7tAbAVNuwwE0rYVVD08bcMMMANiUBhsBuJnQPwLiJ9D+dFQ+bswDQABBgCT4FBPo1peFAAAAABJRU5ErkJggg==</buffer><Offset>3</Offset><BytesRead>77</BytesRead></AppendChunk></soap12:Body></soap12:Envelope> (only this is uploaded)


Dmitry Tsarevich

unread,
Sep 23, 2016, 7:32:45 AM9/23/16
to DevTargetProcess
Hello!

Correct me if i'm wrong. but looks like last chunk parameters is invalid.
Since last but one chunk length is 80 bytes and offset is 240 bytes, you should start last chunk at 320 (240 + 80) bytes offset:


4.
...
<soap12:Body><AppendChunk xmlns="http://targetprocess.com"><fileName>IMAGEN_1.png</fileName><buffer>cKmRBik+Q02mYPPdjPMebqUwYxbhyFPuwybkrcgzWfQRbc9XMdQd6OZwZrXiKHNuJeBn10Qm+aLoEQSBWQsZSAdRINsXoHNdgIApH4FEzSt</buffer><Offset>240</Offset><BytesRead>80</BytesRead></AppendChunk></soap12:Body></soap12:Envelope>

5.
...
<soap12:Body><AppendChunk xmlns="http://targetprocess.com"><fileName>IMAGEN_1.png</fileName><buffer>guL6FZEaX0HV/4Mlz7tAbAVNuwwE0rYVVD08bcMMMANiUBhsBuJnQPwLiJ9D+dFQ+bswDQABBgCT4FBPo1peFAAAAABJRU5ErkJggg==</buffer><Offset>320</Offset><BytesRead>77</BytesRead></AppendChunk></soap12:Body></soap12:Envelope> (only this is uploaded)

An example pseudocode you can use to upload attachment from file by chunks is:

<?php


// Anonymous function (PHP 5.3+). See http://stackoverflow.com/questions/627775/php-pass-function-as-param-then-call-the-function
$appendChunkBySoap
= function ($file_name,    // File name to pass to TP 'AppendChunk' SOAP endpoint.
                               $buffer
,       // Buffer to pass to TP 'AppendChunk' SOAP endpoint.
                               $offset
,       // Offset to pass to TP 'AppendChunk' SOAP endpoint.    
                               $bytes_read
) { // BytesRead to pass to TP 'AppendChunk' SOAP endpoint.
   
// Code to form & send SOAP request to TP 'AppendChunk' SOAP endpoint.


   
// If any error occurs, return FALSE.
   
// echo "Can't append chunk to " . $file_name . " , error: " . error_message;
   
// return FALSE;


   
// return TRUE if all is ok.
   
return TRUE;
};


function uploadAttachmentByChunks($chunk_appender,     // TP attacment upload function.
                                  $attachment_path
,    // Path to attachment file.
                                  $chunk_size
) {       // Size of chunk to upload at once.    
    $file_name
= basename($attachment_path);
   
    $attachment_handle
= fopen($attachment_path, "rb");
   
if (FALSE === $attachment_handle) {
       echo
"Failed to open attachment: " . $attachment_path . "\n";
       
return FALSE;
   
}

    $buffer
= '';
    $offset
= 0;
    $bytes_read
= 0;
    $is_chunk_appended
= TRUE;

   
while (!feof($attachment_handle) && $is_chunk_appended === TRUE) {
       $buffer
= fread($attachment_handle, $chunk_size);
       $bytes_read
= strlen($buffer);
       
       $is_chunk_appended
= $chunk_appender($file_name,
                                            $buffer
,
                                            $offset
,
                                            $bytes_read
);
       
       $offset
+= $bytes_read;
   
}
   
    fclose
($attachment_handle);
   
   
return $is_chunk_appended;
}


// Upload attachment 'my_useful_attachment.zip' by 16384 bytes (16 Kb) chunks.
$upload_result
= uploadAttachmentByChunks($appendChunkBySoap,
                                         
"my_useful_attachment.zip",
                                         
16384);

echo
"Attachment 'my_useful_attachment.zip' is " .
     
($upload_result ? "": "NOT ") .
     
"added to my TP attachments." . "\n";
?>

Hope, this helps :) 
Reply all
Reply to author
Forward
0 new messages