Xbase++
Xbase++
WordPress Media Upload
See more WordPress Examples
Demonstrates how to upload a media file to WordPress.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oReq
LOCAL oResp
LOCAL oJsonResponse
LOCAL oDate_gmt
LOCAL cHref
LOCAL nEmbeddable
LOCAL cName
LOCAL nTemplated
LOCAL nId
LOCAL cDate
LOCAL cGuidRendered
LOCAL cGuidRaw
LOCAL cModified
LOCAL cModified_gmt
LOCAL cSlug
LOCAL cStatus
LOCAL cV_type
LOCAL cLink
LOCAL cTitleRaw
LOCAL cTitleRendered
LOCAL nAuthor
LOCAL cComment_status
LOCAL cPing_status
LOCAL cTemplate
LOCAL cPermalink_template
LOCAL cGenerated_slug
LOCAL cDescriptionRaw
LOCAL cDescriptionRendered
LOCAL cCaptionRaw
LOCAL cCaptionRendered
LOCAL cAlt_text
LOCAL cMedia_type
LOCAL cMime_type
LOCAL nMedia_detailsWidth
LOCAL nMedia_detailsHeight
LOCAL cMedia_detailsFile
LOCAL cMedia_detailsSizesWoocommerce_gallery_thumbnailFile
LOCAL nMedia_detailsSizesWoocommerce_gallery_thumbnailWidth
LOCAL nMedia_detailsSizesWoocommerce_gallery_thumbnailHeight
LOCAL cMedia_detailsSizesWoocommerce_gallery_thumbnailMime_type
LOCAL cMedia_detailsSizesWoocommerce_gallery_thumbnailSource_url
LOCAL cMedia_detailsSizesShop_thumbnailFile
LOCAL nMedia_detailsSizesShop_thumbnailWidth
LOCAL nMedia_detailsSizesShop_thumbnailHeight
LOCAL cMedia_detailsSizesShop_thumbnailMime_type
LOCAL cMedia_detailsSizesShop_thumbnailSource_url
LOCAL cMedia_detailsSizesFullFile
LOCAL nMedia_detailsSizesFullWidth
LOCAL nMedia_detailsSizesFullHeight
LOCAL cMedia_detailsSizesFullMime_type
LOCAL cMedia_detailsSizesFullSource_url
LOCAL cMedia_detailsImage_metaAperture
LOCAL cMedia_detailsImage_metaCredit
LOCAL cMedia_detailsImage_metaCamera
LOCAL cMedia_detailsImage_metaCaption
LOCAL cMedia_detailsImage_metaCreated_timestamp
LOCAL cMedia_detailsImage_metaCopyright
LOCAL cMedia_detailsImage_metaFocal_length
LOCAL cMedia_detailsImage_metaIso
LOCAL cMedia_detailsImage_metaShutter_speed
LOCAL cMedia_detailsImage_metaTitle
LOCAL cMedia_detailsImage_metaOrientation
LOCAL cPost
LOCAL cSource_url
LOCAL i
LOCAL nCount_i
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oHttp := CreateObject("Chilkat.Http")
// 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.
oHttp:Login := "wp_username"
// Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
oHttp:Password := "app_password"
oHttp:BasicAuth := 1
oReq := CreateObject("Chilkat.HttpRequest")
oReq:HttpVerb := "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
oReq:ContentType := "image/jpg"
oReq:Path := "/wp-json/wp/v2/media"
nSuccess := oReq:LoadBodyFromFile("qa_data/jpg/starfish.jpg")
IF (nSuccess == 0)
? oReq:LastErrorText
oHttp:destroy()
oReq:destroy()
RETURN
ENDIF
oReq:AddHeader("Content-Disposition", "attachment; filename=starfish.jpg")
oReq:AddHeader("Expect", "100-continue")
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpSReq("www.yourserver.com", 443, 1, oReq, oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oReq:destroy()
oResp:destroy()
RETURN
ENDIF
? "HTTP response status: " + Str(oResp:StatusCode)
oJsonResponse := CreateObject("Chilkat.JsonObject")
oJsonResponse:Load(oResp:BodyStr)
oJsonResponse:EmitCompact := 0
? oJsonResponse:Emit()
// 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
oDate_gmt := CreateObject("Chilkat.DtObj")
nId := oJsonResponse:IntOf("id")
cDate := oJsonResponse:StringOf("date")
oJsonResponse:DtOf("date_gmt", 0, oDate_gmt)
cGuidRendered := oJsonResponse:StringOf("guid.rendered")
cGuidRaw := oJsonResponse:StringOf("guid.raw")
cModified := oJsonResponse:StringOf("modified")
cModified_gmt := oJsonResponse:StringOf("modified_gmt")
cSlug := oJsonResponse:StringOf("slug")
cStatus := oJsonResponse:StringOf("status")
cV_type := oJsonResponse:StringOf("type")
cLink := oJsonResponse:StringOf("link")
cTitleRaw := oJsonResponse:StringOf("title.raw")
cTitleRendered := oJsonResponse:StringOf("title.rendered")
nAuthor := oJsonResponse:IntOf("author")
cComment_status := oJsonResponse:StringOf("comment_status")
cPing_status := oJsonResponse:StringOf("ping_status")
cTemplate := oJsonResponse:StringOf("template")
cPermalink_template := oJsonResponse:StringOf("permalink_template")
cGenerated_slug := oJsonResponse:StringOf("generated_slug")
cDescriptionRaw := oJsonResponse:StringOf("description.raw")
cDescriptionRendered := oJsonResponse:StringOf("description.rendered")
cCaptionRaw := oJsonResponse:StringOf("caption.raw")
cCaptionRendered := oJsonResponse:StringOf("caption.rendered")
cAlt_text := oJsonResponse:StringOf("alt_text")
cMedia_type := oJsonResponse:StringOf("media_type")
cMime_type := oJsonResponse:StringOf("mime_type")
nMedia_detailsWidth := oJsonResponse:IntOf("media_details.width")
nMedia_detailsHeight := oJsonResponse:IntOf("media_details.height")
cMedia_detailsFile := oJsonResponse:StringOf("media_details.file")
cMedia_detailsSizesWoocommerce_gallery_thumbnailFile := oJsonResponse:StringOf("media_details.sizes.woocommerce_gallery_thumbnail.file")
nMedia_detailsSizesWoocommerce_gallery_thumbnailWidth := oJsonResponse:IntOf("media_details.sizes.woocommerce_gallery_thumbnail.width")
nMedia_detailsSizesWoocommerce_gallery_thumbnailHeight := oJsonResponse:IntOf("media_details.sizes.woocommerce_gallery_thumbnail.height")
cMedia_detailsSizesWoocommerce_gallery_thumbnailMime_type := oJsonResponse:StringOf("media_details.sizes.woocommerce_gallery_thumbnail.mime_type")
cMedia_detailsSizesWoocommerce_gallery_thumbnailSource_url := oJsonResponse:StringOf("media_details.sizes.woocommerce_gallery_thumbnail.source_url")
cMedia_detailsSizesShop_thumbnailFile := oJsonResponse:StringOf("media_details.sizes.shop_thumbnail.file")
nMedia_detailsSizesShop_thumbnailWidth := oJsonResponse:IntOf("media_details.sizes.shop_thumbnail.width")
nMedia_detailsSizesShop_thumbnailHeight := oJsonResponse:IntOf("media_details.sizes.shop_thumbnail.height")
cMedia_detailsSizesShop_thumbnailMime_type := oJsonResponse:StringOf("media_details.sizes.shop_thumbnail.mime_type")
cMedia_detailsSizesShop_thumbnailSource_url := oJsonResponse:StringOf("media_details.sizes.shop_thumbnail.source_url")
cMedia_detailsSizesFullFile := oJsonResponse:StringOf("media_details.sizes.full.file")
nMedia_detailsSizesFullWidth := oJsonResponse:IntOf("media_details.sizes.full.width")
nMedia_detailsSizesFullHeight := oJsonResponse:IntOf("media_details.sizes.full.height")
cMedia_detailsSizesFullMime_type := oJsonResponse:StringOf("media_details.sizes.full.mime_type")
cMedia_detailsSizesFullSource_url := oJsonResponse:StringOf("media_details.sizes.full.source_url")
cMedia_detailsImage_metaAperture := oJsonResponse:StringOf("media_details.image_meta.aperture")
cMedia_detailsImage_metaCredit := oJsonResponse:StringOf("media_details.image_meta.credit")
cMedia_detailsImage_metaCamera := oJsonResponse:StringOf("media_details.image_meta.camera")
cMedia_detailsImage_metaCaption := oJsonResponse:StringOf("media_details.image_meta.caption")
cMedia_detailsImage_metaCreated_timestamp := oJsonResponse:StringOf("media_details.image_meta.created_timestamp")
cMedia_detailsImage_metaCopyright := oJsonResponse:StringOf("media_details.image_meta.copyright")
cMedia_detailsImage_metaFocal_length := oJsonResponse:StringOf("media_details.image_meta.focal_length")
cMedia_detailsImage_metaIso := oJsonResponse:StringOf("media_details.image_meta.iso")
cMedia_detailsImage_metaShutter_speed := oJsonResponse:StringOf("media_details.image_meta.shutter_speed")
cMedia_detailsImage_metaTitle := oJsonResponse:StringOf("media_details.image_meta.title")
cMedia_detailsImage_metaOrientation := oJsonResponse:StringOf("media_details.image_meta.orientation")
cPost := oJsonResponse:StringOf("post")
cSource_url := oJsonResponse:StringOf("source_url")
i := 0
nCount_i := oJsonResponse:SizeOfArray("meta")
DO WHILE i < nCount_i
oJsonResponse:I := i
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("media_details.image_meta.keywords")
DO WHILE i < nCount_i
oJsonResponse:I := i
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("missing_image_sizes")
DO WHILE i < nCount_i
oJsonResponse:I := i
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("_links.self")
DO WHILE i < nCount_i
oJsonResponse:I := i
cHref := oJsonResponse:StringOf("_links.self[i].href")
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("_links.collection")
DO WHILE i < nCount_i
oJsonResponse:I := i
cHref := oJsonResponse:StringOf("_links.collection[i].href")
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("_links.about")
DO WHILE i < nCount_i
oJsonResponse:I := i
cHref := oJsonResponse:StringOf("_links.about[i].href")
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("_links.author")
DO WHILE i < nCount_i
oJsonResponse:I := i
nEmbeddable := oJsonResponse:BoolOf("_links.author[i].embeddable")
cHref := oJsonResponse:StringOf("_links.author[i].href")
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("_links.replies")
DO WHILE i < nCount_i
oJsonResponse:I := i
nEmbeddable := oJsonResponse:BoolOf("_links.replies[i].embeddable")
cHref := oJsonResponse:StringOf("_links.replies[i].href")
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("_links.wp:action-unfiltered-html")
DO WHILE i < nCount_i
oJsonResponse:I := i
cHref := oJsonResponse:StringOf("_links.wp:action-unfiltered-html[i].href")
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("_links.wp:action-assign-author")
DO WHILE i < nCount_i
oJsonResponse:I := i
cHref := oJsonResponse:StringOf("_links.wp:action-assign-author[i].href")
i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("_links.curies")
DO WHILE i < nCount_i
oJsonResponse:I := i
cName := oJsonResponse:StringOf("_links.curies[i].name")
cHref := oJsonResponse:StringOf("_links.curies[i].href")
nTemplated := oJsonResponse:BoolOf("_links.curies[i].templated")
i := i + 1
ENDDO
oHttp:destroy()
oReq:destroy()
oResp:destroy()
oJsonResponse:destroy()
oDate_gmt:destroy()