1

I am currently making a plugin that takes the posts and sends their data to a rest API. Everything else is going smoothly. But I am very confused about how I can achieve this for the featured images. I have to upload a file, not a url. Basically, the API takes multipart/form-data and has an HTTP request method PUT.

The method currently looks something like this:

public function uploadFeaturedImage($post_id)
{   
    $url = build_api_url('posts/' . $post_id. '/photo');
    
    $featured_image_url = get_the_post_thumbnail_url($post_id);
    $image_data =  // Get featured image file here

    $data = array(
        'file' => $image_data
    );

    $args = array(
        'method' => 'PUT',
        'headers' => array('Content-Type' => 'multipart/form-data', 'Authorization' => 'Bearer <token>' ),
        'body' => $data
    );


    $response = wp_remote_request($url,$args);

    return $response['body'];

}
2
  • Please edit your question to include more information. Are you trying to send the actual image to this API, or the URL to the uploaded image? What does the code you're using to send post data look like?
    – Pat J
    Commented Jun 4, 2021 at 13:53
  • Hi Pat, it has to be the actual image file. I updated my question to include some code. Commented Jun 5, 2021 at 5:55

2 Answers 2

1

Here is an idea for sending a featured image (file data) on API and setting the image as a featured image on the server.

Get File data from URL using following function:

/*fetch the file from URL*/
function prefix_get_file_data_from_url( $url ) {
    $response = wp_remote_get( $url );
    if ( is_array( $response ) && ! is_wp_error( $response ) ) {
        return wp_remote_retrieve_body( $response );
    }
    return '';
}

After getting the file send it to the server.

On the server end, you will need the following function to upload the file and set it as a featured image.

Note: not tested.

2
  • I currently used curl to solve this. But this seems a better approach. Could you please complete your answer, taking reference to my answer? Commented Jun 6, 2021 at 12:32
  • You just need to set image data on your above code: $image_data = prefix_get_file_data_from_url($featured_image_url); Commented Jun 6, 2021 at 15:02
0

Don't know if this is a good approach. But I solved this using curl.

public function uploadFeaturedImage($post_id)
{

    $api_id = $this->get_api_id($post_id);

    $url = blog_transporter_build_api_url('posts/' . $api_id . '/photo');

    $image_url = get_the_post_thumbnail_url($post_id);

    if (empty($image_url)) return false;

    $image_id = get_post_thumbnail_id($post_id);
    $mime_type = get_post_mime_type($image_id);

    $data = array('file' => new CURLFile(
        $image_url,
        $mime_type,
        'featured-image'
    ));

    $headers = array('Authorization: Bearer <token>' , 'Content-Type: multipart/form-data');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($ch);
    return $response;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.