Unicode C
Unicode C
WordPress Media Upload
See more WordPress Examples
Demonstrates how to upload a media file to WordPress.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkDtObjW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkHttpRequestW req;
HCkHttpResponseW resp;
HCkJsonObjectW jsonResponse;
HCkDtObjW date_gmt;
const wchar_t *href;
BOOL embeddable;
const wchar_t *name;
BOOL templated;
int id;
const wchar_t *date;
const wchar_t *guidRendered;
const wchar_t *guidRaw;
const wchar_t *modified;
const wchar_t *modified_gmt;
const wchar_t *slug;
const wchar_t *status;
const wchar_t *v_type;
const wchar_t *link;
const wchar_t *titleRaw;
const wchar_t *titleRendered;
int author;
const wchar_t *comment_status;
const wchar_t *ping_status;
const wchar_t *template;
const wchar_t *permalink_template;
const wchar_t *generated_slug;
const wchar_t *descriptionRaw;
const wchar_t *descriptionRendered;
const wchar_t *captionRaw;
const wchar_t *captionRendered;
const wchar_t *alt_text;
const wchar_t *media_type;
const wchar_t *mime_type;
int media_detailsWidth;
int media_detailsHeight;
const wchar_t *media_detailsFile;
const wchar_t *media_detailsSizesWoocommerce_gallery_thumbnailFile;
int media_detailsSizesWoocommerce_gallery_thumbnailWidth;
int media_detailsSizesWoocommerce_gallery_thumbnailHeight;
const wchar_t *media_detailsSizesWoocommerce_gallery_thumbnailMime_type;
const wchar_t *media_detailsSizesWoocommerce_gallery_thumbnailSource_url;
const wchar_t *media_detailsSizesShop_thumbnailFile;
int media_detailsSizesShop_thumbnailWidth;
int media_detailsSizesShop_thumbnailHeight;
const wchar_t *media_detailsSizesShop_thumbnailMime_type;
const wchar_t *media_detailsSizesShop_thumbnailSource_url;
const wchar_t *media_detailsSizesFullFile;
int media_detailsSizesFullWidth;
int media_detailsSizesFullHeight;
const wchar_t *media_detailsSizesFullMime_type;
const wchar_t *media_detailsSizesFullSource_url;
const wchar_t *media_detailsImage_metaAperture;
const wchar_t *media_detailsImage_metaCredit;
const wchar_t *media_detailsImage_metaCamera;
const wchar_t *media_detailsImage_metaCaption;
const wchar_t *media_detailsImage_metaCreated_timestamp;
const wchar_t *media_detailsImage_metaCopyright;
const wchar_t *media_detailsImage_metaFocal_length;
const wchar_t *media_detailsImage_metaIso;
const wchar_t *media_detailsImage_metaShutter_speed;
const wchar_t *media_detailsImage_metaTitle;
const wchar_t *media_detailsImage_metaOrientation;
const wchar_t *post;
const wchar_t *source_url;
int i;
int count_i;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// This example will use WordPress username / Application password authentication using the "Applications Password" plugin.
// See https://wordpress.org/plugins/application-passwords/
// Use your WordPress login, such as "admin", not the application name.
CkHttpW_putLogin(http,L"wp_username");
// Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
CkHttpW_putPassword(http,L"app_password");
CkHttpW_putBasicAuth(http,TRUE);
req = CkHttpRequestW_Create();
CkHttpRequestW_putHttpVerb(req,L"POST");
// Use the Content-Type that corresponds to the type of file you are uploading.
// WordPress supports uploading the following file types:
// Images
//
// .jpg (image/jpg)
// .jpeg (image/jpeg)
// .png (image/png)
// .gif (image/gif)
// .ico (image/x-icon)
//
// Documents
//
// .pdf (applicatin/pdf)
// .doc, .docx (application/msword)
// .ppt, .pptx, .pps, .ppsx (application/mspowerpoint)
// .odt
// .xls, .xlsx (application/msexcel)
// .psd (??? image/vnd.adobe.photoshop,application/x-photoshop,application/photoshop,application/psd,image/psd)
//
// Audio
//
// .mp3 (audio/mpeg)
// .m4a
// .ogg
// .wav (audio/wav)
//
// Video
//
// .mp4, .m4v (video/mp4)
// .mov (video/quicktime)
// .wmv (video/x-ms-wmv)
// .avi (video/avi)
// .mpg (video/mpeg)
// .ogv
// .3gp
// .3g2
CkHttpRequestW_putContentType(req,L"image/jpg");
CkHttpRequestW_putPath(req,L"/wp-json/wp/v2/media");
success = CkHttpRequestW_LoadBodyFromFile(req,L"qa_data/jpg/starfish.jpg");
if (success == FALSE) {
wprintf(L"%s\n",CkHttpRequestW_lastErrorText(req));
CkHttpW_Dispose(http);
CkHttpRequestW_Dispose(req);
return;
}
CkHttpRequestW_AddHeader(req,L"Content-Disposition",L"attachment; filename=starfish.jpg");
CkHttpRequestW_AddHeader(req,L"Expect",L"100-continue");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpSReq(http,L"www.yourserver.com",443,TRUE,req,resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkHttpRequestW_Dispose(req);
CkHttpResponseW_Dispose(resp);
return;
}
wprintf(L"HTTP response status: %d\n",CkHttpResponseW_getStatusCode(resp));
jsonResponse = CkJsonObjectW_Create();
CkJsonObjectW_Load(jsonResponse,CkHttpResponseW_bodyStr(resp));
CkJsonObjectW_putEmitCompact(jsonResponse,FALSE);
wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse));
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "id": 1915,
// "date": "2021-01-19T18:25:01",
// "date_gmt": "2021-01-20T01:25:01",
// "guid": {
// "rendered": "http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg",
// "raw": "http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg"
// },
// "modified": "2021-01-19T18:25:01",
// "modified_gmt": "2021-01-20T01:25:01",
// "slug": "starfish",
// "status": "inherit",
// "type": "attachment",
// "link": "https:\/\/cknotes.com\/starfish\/",
// "title": {
// "raw": "starfish",
// "rendered": "starfish"
// },
// "author": 1,
// "comment_status": "closed",
// "ping_status": "closed",
// "template": "",
// "meta": [
// ],
// "permalink_template": "https:\/\/cknotes.com\/?attachment_id=1915",
// "generated_slug": "starfish",
// "description": {
// "raw": "",
// "rendered": "<p class=\"attachment\"><a href='https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg'><img width=\"120\" height=\"120\" src=\"https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg\" class=\"attachment-medium size-medium\" alt=\"\" loading=\"lazy\" srcset=\"http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg 120w, http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish-100x100.jpg 100w\" sizes=\"(max-width: 120px) 100vw, 120px\" \/><\/a><\/p>\n"
// },
// "caption": {
// "raw": "",
// "rendered": ""
// },
// "alt_text": "",
// "media_type": "image",
// "mime_type": "image\/jpeg",
// "media_details": {
// "width": 120,
// "height": 120,
// "file": "2021\/01\/starfish.jpg",
// "sizes": {
// "woocommerce_gallery_thumbnail": {
// "file": "starfish-100x100.jpg",
// "width": 100,
// "height": 100,
// "mime_type": "image\/jpeg",
// "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish-100x100.jpg"
// },
// "shop_thumbnail": {
// "file": "starfish-100x100.jpg",
// "width": 100,
// "height": 100,
// "mime_type": "image\/jpeg",
// "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish-100x100.jpg"
// },
// "full": {
// "file": "starfish.jpg",
// "width": 120,
// "height": 120,
// "mime_type": "image\/jpeg",
// "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg"
// }
// },
// "image_meta": {
// "aperture": "0",
// "credit": "",
// "camera": "",
// "caption": "",
// "created_timestamp": "0",
// "copyright": "",
// "focal_length": "0",
// "iso": "0",
// "shutter_speed": "0",
// "title": "",
// "orientation": "0",
// "keywords": [
// ]
// }
// },
// "post": null,
// "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg",
// "missing_image_sizes": [
// ],
// "_links": {
// "self": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media\/1915"
// }
// ],
// "collection": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media"
// }
// ],
// "about": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/attachment"
// }
// ],
// "author": [
// {
// "embeddable": true,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"
// }
// ],
// "replies": [
// {
// "embeddable": true,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=1915"
// }
// ],
// "wp:action-unfiltered-html": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media\/1915"
// }
// ],
// "wp:action-assign-author": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media\/1915"
// }
// ],
// "curies": [
// {
// "name": "wp",
// "href": "https:\/\/api.w.org\/{rel}",
// "templated": true
// }
// ]
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
date_gmt = CkDtObjW_Create();
id = CkJsonObjectW_IntOf(jsonResponse,L"id");
date = CkJsonObjectW_stringOf(jsonResponse,L"date");
CkJsonObjectW_DtOf(jsonResponse,L"date_gmt",FALSE,date_gmt);
guidRendered = CkJsonObjectW_stringOf(jsonResponse,L"guid.rendered");
guidRaw = CkJsonObjectW_stringOf(jsonResponse,L"guid.raw");
modified = CkJsonObjectW_stringOf(jsonResponse,L"modified");
modified_gmt = CkJsonObjectW_stringOf(jsonResponse,L"modified_gmt");
slug = CkJsonObjectW_stringOf(jsonResponse,L"slug");
status = CkJsonObjectW_stringOf(jsonResponse,L"status");
v_type = CkJsonObjectW_stringOf(jsonResponse,L"type");
link = CkJsonObjectW_stringOf(jsonResponse,L"link");
titleRaw = CkJsonObjectW_stringOf(jsonResponse,L"title.raw");
titleRendered = CkJsonObjectW_stringOf(jsonResponse,L"title.rendered");
author = CkJsonObjectW_IntOf(jsonResponse,L"author");
comment_status = CkJsonObjectW_stringOf(jsonResponse,L"comment_status");
ping_status = CkJsonObjectW_stringOf(jsonResponse,L"ping_status");
template = CkJsonObjectW_stringOf(jsonResponse,L"template");
permalink_template = CkJsonObjectW_stringOf(jsonResponse,L"permalink_template");
generated_slug = CkJsonObjectW_stringOf(jsonResponse,L"generated_slug");
descriptionRaw = CkJsonObjectW_stringOf(jsonResponse,L"description.raw");
descriptionRendered = CkJsonObjectW_stringOf(jsonResponse,L"description.rendered");
captionRaw = CkJsonObjectW_stringOf(jsonResponse,L"caption.raw");
captionRendered = CkJsonObjectW_stringOf(jsonResponse,L"caption.rendered");
alt_text = CkJsonObjectW_stringOf(jsonResponse,L"alt_text");
media_type = CkJsonObjectW_stringOf(jsonResponse,L"media_type");
mime_type = CkJsonObjectW_stringOf(jsonResponse,L"mime_type");
media_detailsWidth = CkJsonObjectW_IntOf(jsonResponse,L"media_details.width");
media_detailsHeight = CkJsonObjectW_IntOf(jsonResponse,L"media_details.height");
media_detailsFile = CkJsonObjectW_stringOf(jsonResponse,L"media_details.file");
media_detailsSizesWoocommerce_gallery_thumbnailFile = CkJsonObjectW_stringOf(jsonResponse,L"media_details.sizes.woocommerce_gallery_thumbnail.file");
media_detailsSizesWoocommerce_gallery_thumbnailWidth = CkJsonObjectW_IntOf(jsonResponse,L"media_details.sizes.woocommerce_gallery_thumbnail.width");
media_detailsSizesWoocommerce_gallery_thumbnailHeight = CkJsonObjectW_IntOf(jsonResponse,L"media_details.sizes.woocommerce_gallery_thumbnail.height");
media_detailsSizesWoocommerce_gallery_thumbnailMime_type = CkJsonObjectW_stringOf(jsonResponse,L"media_details.sizes.woocommerce_gallery_thumbnail.mime_type");
media_detailsSizesWoocommerce_gallery_thumbnailSource_url = CkJsonObjectW_stringOf(jsonResponse,L"media_details.sizes.woocommerce_gallery_thumbnail.source_url");
media_detailsSizesShop_thumbnailFile = CkJsonObjectW_stringOf(jsonResponse,L"media_details.sizes.shop_thumbnail.file");
media_detailsSizesShop_thumbnailWidth = CkJsonObjectW_IntOf(jsonResponse,L"media_details.sizes.shop_thumbnail.width");
media_detailsSizesShop_thumbnailHeight = CkJsonObjectW_IntOf(jsonResponse,L"media_details.sizes.shop_thumbnail.height");
media_detailsSizesShop_thumbnailMime_type = CkJsonObjectW_stringOf(jsonResponse,L"media_details.sizes.shop_thumbnail.mime_type");
media_detailsSizesShop_thumbnailSource_url = CkJsonObjectW_stringOf(jsonResponse,L"media_details.sizes.shop_thumbnail.source_url");
media_detailsSizesFullFile = CkJsonObjectW_stringOf(jsonResponse,L"media_details.sizes.full.file");
media_detailsSizesFullWidth = CkJsonObjectW_IntOf(jsonResponse,L"media_details.sizes.full.width");
media_detailsSizesFullHeight = CkJsonObjectW_IntOf(jsonResponse,L"media_details.sizes.full.height");
media_detailsSizesFullMime_type = CkJsonObjectW_stringOf(jsonResponse,L"media_details.sizes.full.mime_type");
media_detailsSizesFullSource_url = CkJsonObjectW_stringOf(jsonResponse,L"media_details.sizes.full.source_url");
media_detailsImage_metaAperture = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.aperture");
media_detailsImage_metaCredit = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.credit");
media_detailsImage_metaCamera = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.camera");
media_detailsImage_metaCaption = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.caption");
media_detailsImage_metaCreated_timestamp = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.created_timestamp");
media_detailsImage_metaCopyright = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.copyright");
media_detailsImage_metaFocal_length = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.focal_length");
media_detailsImage_metaIso = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.iso");
media_detailsImage_metaShutter_speed = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.shutter_speed");
media_detailsImage_metaTitle = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.title");
media_detailsImage_metaOrientation = CkJsonObjectW_stringOf(jsonResponse,L"media_details.image_meta.orientation");
post = CkJsonObjectW_stringOf(jsonResponse,L"post");
source_url = CkJsonObjectW_stringOf(jsonResponse,L"source_url");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"meta");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"media_details.image_meta.keywords");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"missing_image_sizes");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"_links.self");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
href = CkJsonObjectW_stringOf(jsonResponse,L"_links.self[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"_links.collection");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
href = CkJsonObjectW_stringOf(jsonResponse,L"_links.collection[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"_links.about");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
href = CkJsonObjectW_stringOf(jsonResponse,L"_links.about[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"_links.author");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
embeddable = CkJsonObjectW_BoolOf(jsonResponse,L"_links.author[i].embeddable");
href = CkJsonObjectW_stringOf(jsonResponse,L"_links.author[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"_links.replies");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
embeddable = CkJsonObjectW_BoolOf(jsonResponse,L"_links.replies[i].embeddable");
href = CkJsonObjectW_stringOf(jsonResponse,L"_links.replies[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"_links.wp:action-unfiltered-html");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
href = CkJsonObjectW_stringOf(jsonResponse,L"_links.wp:action-unfiltered-html[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"_links.wp:action-assign-author");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
href = CkJsonObjectW_stringOf(jsonResponse,L"_links.wp:action-assign-author[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"_links.curies");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
name = CkJsonObjectW_stringOf(jsonResponse,L"_links.curies[i].name");
href = CkJsonObjectW_stringOf(jsonResponse,L"_links.curies[i].href");
templated = CkJsonObjectW_BoolOf(jsonResponse,L"_links.curies[i].templated");
i = i + 1;
}
CkHttpW_Dispose(http);
CkHttpRequestW_Dispose(req);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(jsonResponse);
CkDtObjW_Dispose(date_gmt);
}