radishconcepts of Radish Concepts
12/20/2019 - 10:25 AM

Sending files via CURL

Using CURL, since wp_remote_post() does not support sending of files.

<?php

$filename = '/path/to/file.pot';
$filename = new \CURLFile( $filename, 'text/x-po' );

$post_data = [
	'your_key'  => $your_value,
	'file' => $filename,
];

$options = [
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_POSTFIELDS     => $post_data,
	CURLOPT_CUSTOMREQUEST  => 'POST',
	CURLOPT_HTTPHEADER     => [ 'Content-Type: multipart/form-data' ],
];

$ch = curl_init( $your_api_url );
curl_setopt_array( $ch, $options );
$result = curl_exec( $ch );
curl_close( $ch );