Skip to content

Instantly share code, notes, and snippets.

@kagg-design
Created April 7, 2021 15:47
Show Gist options
  • Save kagg-design/6636d750c832f0f13405b4b06a78d65b to your computer and use it in GitHub Desktop.
Save kagg-design/6636d750c832f0f13405b4b06a78d65b to your computer and use it in GitHub Desktop.
Example of how to send files to the remote server.
$boundary = str_repeat( '-', 24 ) . wp_generate_password( 16 );
$headers = array(
'content-type' => 'multipart/form-data; boundary=' . $boundary,
);
if ( ! isset( $files['path'] ) ) {
$error_message = 'Invalid arguments';
return array( $success, $error_message, $word_count, $individual_word_count );
}
$body = '';
foreach ( $files['path'] as $index => $file_path ) {
$body .= '--' . $boundary . "\r\n";
if ( 0 === $index ) {
$name = 'file';
} else {
$name = $index - 1;
}
$body .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n";
$body .= 'Content-Type: ' . $files['type'][ $index ] . "\r\n\r\n";
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$body .= file_get_contents( $file_path ) . "\r\n";
}
$body .= '--' . $boundary . '--' . "\r\n";
$max_execution_time = 600;
set_time_limit( $max_execution_time );
$timeout = ini_get( 'max_execution_time' ) - 5;
$timeout = $timeout < $max_execution_time - 5 ? $max_execution_time - 5 : $timeout;
$remote_args = array(
'method' => 'POST',
'headers' => $headers,
'redirection' => 0,
'body' => $body,
'timeout' => $timeout,
);
$word_count_ip_address = fq_get_option( 'word_count_ip_address' );
$result = wp_remote_post( 'http://' . $word_count_ip_address . '/a/uploadMultiple', $remote_args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment