Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Lianja) WordPress Media UploadSee more WordPress ExamplesDemonstrates how to upload a media file to WordPress. For more information, see https://developer.wordpress.org/rest-api/reference/media/#create-a-media-item
// This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loHttp = createobject("CkHttp") // 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. loHttp.Login = "wp_username" // Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq" loHttp.Password = "app_password" loHttp.BasicAuth = .T. loReq = createobject("CkHttpRequest") loReq.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 loReq.ContentType = "image/jpg" loReq.Path = "/wp-json/wp/v2/media" llSuccess = loReq.LoadBodyFromFile("qa_data/jpg/starfish.jpg") if (llSuccess = .F.) then ? loReq.LastErrorText release loHttp release loReq return endif loReq.AddHeader("Content-Disposition","attachment; filename=starfish.jpg") loReq.AddHeader("Expect","100-continue") loResp = loHttp.SynchronousRequest("www.yourserver.com",443,.T.,loReq) if (loHttp.LastMethodSuccess <> .T.) then ? loHttp.LastErrorText release loHttp release loReq return endif ? "HTTP response status: " + str(loResp.StatusCode) loJsonResponse = createobject("CkJsonObject") loJsonResponse.Load(loResp.BodyStr) release loResp loJsonResponse.EmitCompact = .F. ? loJsonResponse.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 loDate_gmt = createobject("CkDtObj") lnId = loJsonResponse.IntOf("id") lcDate = loJsonResponse.StringOf("date") loJsonResponse.DtOf("date_gmt",.F.,loDate_gmt) lcGuidRendered = loJsonResponse.StringOf("guid.rendered") lcGuidRaw = loJsonResponse.StringOf("guid.raw") lcModified = loJsonResponse.StringOf("modified") lcModified_gmt = loJsonResponse.StringOf("modified_gmt") lcSlug = loJsonResponse.StringOf("slug") lcStatus = loJsonResponse.StringOf("status") lcV_type = loJsonResponse.StringOf("type") lcLink = loJsonResponse.StringOf("link") lcTitleRaw = loJsonResponse.StringOf("title.raw") lcTitleRendered = loJsonResponse.StringOf("title.rendered") lnAuthor = loJsonResponse.IntOf("author") lcComment_status = loJsonResponse.StringOf("comment_status") lcPing_status = loJsonResponse.StringOf("ping_status") lcTemplate = loJsonResponse.StringOf("template") lcPermalink_template = loJsonResponse.StringOf("permalink_template") lcGenerated_slug = loJsonResponse.StringOf("generated_slug") lcDescriptionRaw = loJsonResponse.StringOf("description.raw") lcDescriptionRendered = loJsonResponse.StringOf("description.rendered") lcCaptionRaw = loJsonResponse.StringOf("caption.raw") lcCaptionRendered = loJsonResponse.StringOf("caption.rendered") lcAlt_text = loJsonResponse.StringOf("alt_text") lcMedia_type = loJsonResponse.StringOf("media_type") lcMime_type = loJsonResponse.StringOf("mime_type") lnMedia_detailsWidth = loJsonResponse.IntOf("media_details.width") lnMedia_detailsHeight = loJsonResponse.IntOf("media_details.height") lcMedia_detailsFile = loJsonResponse.StringOf("media_details.file") lcMedia_detailsSizesWoocommerce_gallery_thumbnailFile = loJsonResponse.StringOf("media_details.sizes.woocommerce_gallery_thumbnail.file") lnMedia_detailsSizesWoocommerce_gallery_thumbnailWidth = loJsonResponse.IntOf("media_details.sizes.woocommerce_gallery_thumbnail.width") lnMedia_detailsSizesWoocommerce_gallery_thumbnailHeight = loJsonResponse.IntOf("media_details.sizes.woocommerce_gallery_thumbnail.height") lcMedia_detailsSizesWoocommerce_gallery_thumbnailMime_type = loJsonResponse.StringOf("media_details.sizes.woocommerce_gallery_thumbnail.mime_type") lcMedia_detailsSizesWoocommerce_gallery_thumbnailSource_url = loJsonResponse.StringOf("media_details.sizes.woocommerce_gallery_thumbnail.source_url") lcMedia_detailsSizesShop_thumbnailFile = loJsonResponse.StringOf("media_details.sizes.shop_thumbnail.file") lnMedia_detailsSizesShop_thumbnailWidth = loJsonResponse.IntOf("media_details.sizes.shop_thumbnail.width") lnMedia_detailsSizesShop_thumbnailHeight = loJsonResponse.IntOf("media_details.sizes.shop_thumbnail.height") lcMedia_detailsSizesShop_thumbnailMime_type = loJsonResponse.StringOf("media_details.sizes.shop_thumbnail.mime_type") lcMedia_detailsSizesShop_thumbnailSource_url = loJsonResponse.StringOf("media_details.sizes.shop_thumbnail.source_url") lcMedia_detailsSizesFullFile = loJsonResponse.StringOf("media_details.sizes.full.file") lnMedia_detailsSizesFullWidth = loJsonResponse.IntOf("media_details.sizes.full.width") lnMedia_detailsSizesFullHeight = loJsonResponse.IntOf("media_details.sizes.full.height") lcMedia_detailsSizesFullMime_type = loJsonResponse.StringOf("media_details.sizes.full.mime_type") lcMedia_detailsSizesFullSource_url = loJsonResponse.StringOf("media_details.sizes.full.source_url") lcMedia_detailsImage_metaAperture = loJsonResponse.StringOf("media_details.image_meta.aperture") lcMedia_detailsImage_metaCredit = loJsonResponse.StringOf("media_details.image_meta.credit") lcMedia_detailsImage_metaCamera = loJsonResponse.StringOf("media_details.image_meta.camera") lcMedia_detailsImage_metaCaption = loJsonResponse.StringOf("media_details.image_meta.caption") lcMedia_detailsImage_metaCreated_timestamp = loJsonResponse.StringOf("media_details.image_meta.created_timestamp") lcMedia_detailsImage_metaCopyright = loJsonResponse.StringOf("media_details.image_meta.copyright") lcMedia_detailsImage_metaFocal_length = loJsonResponse.StringOf("media_details.image_meta.focal_length") lcMedia_detailsImage_metaIso = loJsonResponse.StringOf("media_details.image_meta.iso") lcMedia_detailsImage_metaShutter_speed = loJsonResponse.StringOf("media_details.image_meta.shutter_speed") lcMedia_detailsImage_metaTitle = loJsonResponse.StringOf("media_details.image_meta.title") lcMedia_detailsImage_metaOrientation = loJsonResponse.StringOf("media_details.image_meta.orientation") lcPost = loJsonResponse.StringOf("post") lcSource_url = loJsonResponse.StringOf("source_url") i = 0 lnCount_i = loJsonResponse.SizeOfArray("meta") do while i < lnCount_i loJsonResponse.I = i i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("media_details.image_meta.keywords") do while i < lnCount_i loJsonResponse.I = i i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("missing_image_sizes") do while i < lnCount_i loJsonResponse.I = i i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("_links.self") do while i < lnCount_i loJsonResponse.I = i lcHref = loJsonResponse.StringOf("_links.self[i].href") i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("_links.collection") do while i < lnCount_i loJsonResponse.I = i lcHref = loJsonResponse.StringOf("_links.collection[i].href") i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("_links.about") do while i < lnCount_i loJsonResponse.I = i lcHref = loJsonResponse.StringOf("_links.about[i].href") i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("_links.author") do while i < lnCount_i loJsonResponse.I = i llEmbeddable = loJsonResponse.BoolOf("_links.author[i].embeddable") lcHref = loJsonResponse.StringOf("_links.author[i].href") i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("_links.replies") do while i < lnCount_i loJsonResponse.I = i llEmbeddable = loJsonResponse.BoolOf("_links.replies[i].embeddable") lcHref = loJsonResponse.StringOf("_links.replies[i].href") i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("_links.wp:action-unfiltered-html") do while i < lnCount_i loJsonResponse.I = i lcHref = loJsonResponse.StringOf("_links.wp:action-unfiltered-html[i].href") i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("_links.wp:action-assign-author") do while i < lnCount_i loJsonResponse.I = i lcHref = loJsonResponse.StringOf("_links.wp:action-assign-author[i].href") i = i + 1 enddo i = 0 lnCount_i = loJsonResponse.SizeOfArray("_links.curies") do while i < lnCount_i loJsonResponse.I = i lcName = loJsonResponse.StringOf("_links.curies[i].name") lcHref = loJsonResponse.StringOf("_links.curies[i].href") llTemplated = loJsonResponse.BoolOf("_links.curies[i].templated") i = i + 1 enddo release loHttp release loReq release loJsonResponse release loDate_gmt |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.