C
C
WordPress Media Upload
See more WordPress Examples
Demonstrates how to upload a media file to WordPress.Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>
#include <C_CkDtObj.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkHttpRequest req;
HCkHttpResponse resp;
HCkJsonObject jsonResponse;
HCkDtObj date_gmt;
const char *href;
BOOL embeddable;
const char *name;
BOOL templated;
int id;
const char *date;
const char *guidRendered;
const char *guidRaw;
const char *modified;
const char *modified_gmt;
const char *slug;
const char *status;
const char *v_type;
const char *link;
const char *titleRaw;
const char *titleRendered;
int author;
const char *comment_status;
const char *ping_status;
const char *template;
const char *permalink_template;
const char *generated_slug;
const char *descriptionRaw;
const char *descriptionRendered;
const char *captionRaw;
const char *captionRendered;
const char *alt_text;
const char *media_type;
const char *mime_type;
int media_detailsWidth;
int media_detailsHeight;
const char *media_detailsFile;
const char *media_detailsSizesWoocommerce_gallery_thumbnailFile;
int media_detailsSizesWoocommerce_gallery_thumbnailWidth;
int media_detailsSizesWoocommerce_gallery_thumbnailHeight;
const char *media_detailsSizesWoocommerce_gallery_thumbnailMime_type;
const char *media_detailsSizesWoocommerce_gallery_thumbnailSource_url;
const char *media_detailsSizesShop_thumbnailFile;
int media_detailsSizesShop_thumbnailWidth;
int media_detailsSizesShop_thumbnailHeight;
const char *media_detailsSizesShop_thumbnailMime_type;
const char *media_detailsSizesShop_thumbnailSource_url;
const char *media_detailsSizesFullFile;
int media_detailsSizesFullWidth;
int media_detailsSizesFullHeight;
const char *media_detailsSizesFullMime_type;
const char *media_detailsSizesFullSource_url;
const char *media_detailsImage_metaAperture;
const char *media_detailsImage_metaCredit;
const char *media_detailsImage_metaCamera;
const char *media_detailsImage_metaCaption;
const char *media_detailsImage_metaCreated_timestamp;
const char *media_detailsImage_metaCopyright;
const char *media_detailsImage_metaFocal_length;
const char *media_detailsImage_metaIso;
const char *media_detailsImage_metaShutter_speed;
const char *media_detailsImage_metaTitle;
const char *media_detailsImage_metaOrientation;
const char *post;
const char *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 = CkHttp_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.
CkHttp_putLogin(http,"wp_username");
// Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
CkHttp_putPassword(http,"app_password");
CkHttp_putBasicAuth(http,TRUE);
req = CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(req,"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
CkHttpRequest_putContentType(req,"image/jpg");
CkHttpRequest_putPath(req,"/wp-json/wp/v2/media");
success = CkHttpRequest_LoadBodyFromFile(req,"qa_data/jpg/starfish.jpg");
if (success == FALSE) {
printf("%s\n",CkHttpRequest_lastErrorText(req));
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
return;
}
CkHttpRequest_AddHeader(req,"Content-Disposition","attachment; filename=starfish.jpg");
CkHttpRequest_AddHeader(req,"Expect","100-continue");
resp = CkHttpResponse_Create();
success = CkHttp_HttpSReq(http,"www.yourserver.com",443,TRUE,req,resp);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
return;
}
printf("HTTP response status: %d\n",CkHttpResponse_getStatusCode(resp));
jsonResponse = CkJsonObject_Create();
CkJsonObject_Load(jsonResponse,CkHttpResponse_bodyStr(resp));
CkJsonObject_putEmitCompact(jsonResponse,FALSE);
printf("%s\n",CkJsonObject_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 = CkDtObj_Create();
id = CkJsonObject_IntOf(jsonResponse,"id");
date = CkJsonObject_stringOf(jsonResponse,"date");
CkJsonObject_DtOf(jsonResponse,"date_gmt",FALSE,date_gmt);
guidRendered = CkJsonObject_stringOf(jsonResponse,"guid.rendered");
guidRaw = CkJsonObject_stringOf(jsonResponse,"guid.raw");
modified = CkJsonObject_stringOf(jsonResponse,"modified");
modified_gmt = CkJsonObject_stringOf(jsonResponse,"modified_gmt");
slug = CkJsonObject_stringOf(jsonResponse,"slug");
status = CkJsonObject_stringOf(jsonResponse,"status");
v_type = CkJsonObject_stringOf(jsonResponse,"type");
link = CkJsonObject_stringOf(jsonResponse,"link");
titleRaw = CkJsonObject_stringOf(jsonResponse,"title.raw");
titleRendered = CkJsonObject_stringOf(jsonResponse,"title.rendered");
author = CkJsonObject_IntOf(jsonResponse,"author");
comment_status = CkJsonObject_stringOf(jsonResponse,"comment_status");
ping_status = CkJsonObject_stringOf(jsonResponse,"ping_status");
template = CkJsonObject_stringOf(jsonResponse,"template");
permalink_template = CkJsonObject_stringOf(jsonResponse,"permalink_template");
generated_slug = CkJsonObject_stringOf(jsonResponse,"generated_slug");
descriptionRaw = CkJsonObject_stringOf(jsonResponse,"description.raw");
descriptionRendered = CkJsonObject_stringOf(jsonResponse,"description.rendered");
captionRaw = CkJsonObject_stringOf(jsonResponse,"caption.raw");
captionRendered = CkJsonObject_stringOf(jsonResponse,"caption.rendered");
alt_text = CkJsonObject_stringOf(jsonResponse,"alt_text");
media_type = CkJsonObject_stringOf(jsonResponse,"media_type");
mime_type = CkJsonObject_stringOf(jsonResponse,"mime_type");
media_detailsWidth = CkJsonObject_IntOf(jsonResponse,"media_details.width");
media_detailsHeight = CkJsonObject_IntOf(jsonResponse,"media_details.height");
media_detailsFile = CkJsonObject_stringOf(jsonResponse,"media_details.file");
media_detailsSizesWoocommerce_gallery_thumbnailFile = CkJsonObject_stringOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.file");
media_detailsSizesWoocommerce_gallery_thumbnailWidth = CkJsonObject_IntOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.width");
media_detailsSizesWoocommerce_gallery_thumbnailHeight = CkJsonObject_IntOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.height");
media_detailsSizesWoocommerce_gallery_thumbnailMime_type = CkJsonObject_stringOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.mime_type");
media_detailsSizesWoocommerce_gallery_thumbnailSource_url = CkJsonObject_stringOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.source_url");
media_detailsSizesShop_thumbnailFile = CkJsonObject_stringOf(jsonResponse,"media_details.sizes.shop_thumbnail.file");
media_detailsSizesShop_thumbnailWidth = CkJsonObject_IntOf(jsonResponse,"media_details.sizes.shop_thumbnail.width");
media_detailsSizesShop_thumbnailHeight = CkJsonObject_IntOf(jsonResponse,"media_details.sizes.shop_thumbnail.height");
media_detailsSizesShop_thumbnailMime_type = CkJsonObject_stringOf(jsonResponse,"media_details.sizes.shop_thumbnail.mime_type");
media_detailsSizesShop_thumbnailSource_url = CkJsonObject_stringOf(jsonResponse,"media_details.sizes.shop_thumbnail.source_url");
media_detailsSizesFullFile = CkJsonObject_stringOf(jsonResponse,"media_details.sizes.full.file");
media_detailsSizesFullWidth = CkJsonObject_IntOf(jsonResponse,"media_details.sizes.full.width");
media_detailsSizesFullHeight = CkJsonObject_IntOf(jsonResponse,"media_details.sizes.full.height");
media_detailsSizesFullMime_type = CkJsonObject_stringOf(jsonResponse,"media_details.sizes.full.mime_type");
media_detailsSizesFullSource_url = CkJsonObject_stringOf(jsonResponse,"media_details.sizes.full.source_url");
media_detailsImage_metaAperture = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.aperture");
media_detailsImage_metaCredit = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.credit");
media_detailsImage_metaCamera = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.camera");
media_detailsImage_metaCaption = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.caption");
media_detailsImage_metaCreated_timestamp = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.created_timestamp");
media_detailsImage_metaCopyright = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.copyright");
media_detailsImage_metaFocal_length = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.focal_length");
media_detailsImage_metaIso = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.iso");
media_detailsImage_metaShutter_speed = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.shutter_speed");
media_detailsImage_metaTitle = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.title");
media_detailsImage_metaOrientation = CkJsonObject_stringOf(jsonResponse,"media_details.image_meta.orientation");
post = CkJsonObject_stringOf(jsonResponse,"post");
source_url = CkJsonObject_stringOf(jsonResponse,"source_url");
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"meta");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"media_details.image_meta.keywords");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"missing_image_sizes");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"_links.self");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
href = CkJsonObject_stringOf(jsonResponse,"_links.self[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"_links.collection");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
href = CkJsonObject_stringOf(jsonResponse,"_links.collection[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"_links.about");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
href = CkJsonObject_stringOf(jsonResponse,"_links.about[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"_links.author");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
embeddable = CkJsonObject_BoolOf(jsonResponse,"_links.author[i].embeddable");
href = CkJsonObject_stringOf(jsonResponse,"_links.author[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"_links.replies");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
embeddable = CkJsonObject_BoolOf(jsonResponse,"_links.replies[i].embeddable");
href = CkJsonObject_stringOf(jsonResponse,"_links.replies[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"_links.wp:action-unfiltered-html");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
href = CkJsonObject_stringOf(jsonResponse,"_links.wp:action-unfiltered-html[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"_links.wp:action-assign-author");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
href = CkJsonObject_stringOf(jsonResponse,"_links.wp:action-assign-author[i].href");
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonResponse,"_links.curies");
while (i < count_i) {
CkJsonObject_putI(jsonResponse,i);
name = CkJsonObject_stringOf(jsonResponse,"_links.curies[i].name");
href = CkJsonObject_stringOf(jsonResponse,"_links.curies[i].href");
templated = CkJsonObject_BoolOf(jsonResponse,"_links.curies[i].templated");
i = i + 1;
}
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(jsonResponse);
CkDtObj_Dispose(date_gmt);
}