php - Adding featured image in wordpress.com via api -


i want add new post featured image, firstly add image 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 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 copied examples , 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/ found code add media 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 form content-type

"(...)to upload media, entire request should multipart/form-data"

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

the media parameter in api call used uploading local image files, you're calling external url. should using media_urls parameter instead. relevant bit from documentation:

media: array of images attach post. upload media, entire request should multipart/form-data encoded. multiple media items displayed in gallery. accepts images (image/gif, image/jpeg, image/png) only.

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

media_urls: array of urls images attach post. sideloads media in post.

your code change to:

... 'content' => http_build_query(        array (         'title' => $title,         'content' => $content,         'tags' => $tags_array,         'categories' => $categories_array,         'media_urls' => array($featuredimage)     ) ... 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -