Wp_remote_post with file load

I am trying to send a file through wp_remote_post . Unfortunately, any file stream or file path (with the addition of CURL style @ or not) passed to this function is simply deleted and removed from the payload.

I found a post on wp-hackers , however it is extremely hacked and error prone. Is there no way to transfer a file through this function without writing the full HTTP payload from scratch?

Here's an example code block using the CURL style (tentative path with @) if that is interesting:

    $body["attachment[{$i}]"] = "@{$attachment}";

    $data = array(
    'body' => $body,
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( "user:{$apiKey}" )));

$url = "https://api.someservice.net/{$domain}/endpoint";
$response = wp_remote_post( $url, $data );

Thanks!

+4
source share
1 answer

Mail data should be sent to the body as an array. Data Transfer Example:

$response = wp_remote_post( $url, array(
    'method' => 'POST',
    'timeout' => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking' => true,
    'headers' => array(),
    'body' => array( 'username' => 'bob', 'password' => '1234xyz' ),
    'cookies' => array()
    )
);

    if ( is_wp_error( $response ) ) {
   $error_message = $response->get_error_message();
   echo "Something went wrong: $error_message";
} else {
   echo 'Response:<pre>';
   print_r( $response );
   echo '</pre>';
}

$response ['body'] , .

0

All Articles