4

I want to add new post with featured image, but firstly add image to a post.

function add_post($access_key,$blogid,$title,$content,$categories_array,$tags_array,$featuredimage)
    {
    $options  = array (
      'http' => 
      array (
        'ignore_errors' => true,
        'method' => 'POST',
        'header' => 
        array (
          0 => 'authorization: Bearer '.$access_key,
          1 => 'Content-Type: multipart/form-data',
        ),
        'content' => http_build_query(   
          array (
            'title' => $title,
            'content' => $content,
            'tags' => $tags_array,
            'categories' => $categories_array,
            'media'=>$featuredimage,///array($featuredimage),//jak nie zadziala to zapakowac w array
            'media[]'=>$featuredimage//array($featuredimage)
          )
        ),
      ),
    );

    $context  = stream_context_create( $options );
    $response = file_get_contents(
      "https://public-api.wordpress.com/rest/v1/sites/{$blogid}/posts/new/",
      false,
      $context
    );
    $response = json_decode( $response );
    return $response;
    }

function body was copied from examples and works fine except adding media

add_post($_GET['token'],$blog_id,"tytul","tresc",array("cat1"),array("tagt1","tag2"), "http://icons.iconarchive.com/icons/iconka/meow/256/cat-walk-icon.png");

add posts without adding image

in documentation
http://developer.wordpress.com/docs/api/1/post/sites/$site/posts/new/ I found only code for add media from console

curl \
--form 'title=Image' \
--form 'media[]=@/path/to/file.jpg' \
-H 'Authorization: BEARER your-token' \
'https://public-api.wordpress.com/rest/v1/sites/123/posts/new'

and mention about form content-type

"(...)To upload media, the entire request should be multipart/form-data"

but when I changed "application/x-www-form-urlencoded" to "multipart/form-data" ...and nothing changed

2

1 Answer 1

2

The media parameter in that API call is used only for uploading local image files, but you're calling it with an external URL. You should be using the media_urls parameter instead. Relevant bit from the documentation:

media: An array of images to attach to the post. To upload media, the entire request should be multipart/form-data encoded. Multiple media items will be displayed in a gallery. Accepts images (image/gif, image/jpeg, image/png) only.

 Example: curl --form 'title=Image' --form 'media[]=@/path/to/file.jpg' ...

media_urls: An array of URLs for images to attach to a post. Sideloads the media in for a post.

Your code could change to:

...
'content' => http_build_query(   
    array (
        'title' => $title,
        'content' => $content,
        'tags' => $tags_array,
        'categories' => $categories_array,
        'media_urls' => array($featuredimage)
    )
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.