我想添加带有特色图片的新帖子,但首先将图片添加到帖子中。
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;
}
函数体是从示例中复制的,除了添加媒体外,工作正常
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");
添加帖子而不添加图像
在文档
http://developer.wordpress.com/docs/api/1/post/sites/ $site/posts/new/ 我发现只有从控制台添加媒体的代码
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'
并提及表单内容类型
“(...)上传媒体,整个请求应该是多部分/表单数据”
但是当我将“application/x-www-form-urlencoded”更改为“multipart/form-data”时......没有任何改变