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
(Xojo Plugin) 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. Dim http As New 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. http.Login = "wp_username" // Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq" http.Password = "app_password" http.BasicAuth = True Dim req As New Chilkat.HttpRequest req.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 req.ContentType = "image/jpg" req.Path = "/wp-json/wp/v2/media" Dim success As Boolean success = req.LoadBodyFromFile("qa_data/jpg/starfish.jpg") If (success = False) Then System.DebugLog(req.LastErrorText) Return End If req.AddHeader "Content-Disposition","attachment; filename=starfish.jpg" req.AddHeader "Expect","100-continue" Dim resp As Chilkat.HttpResponse resp = http.SynchronousRequest("www.yourserver.com",443,True,req) If (http.LastMethodSuccess <> True) Then System.DebugLog(http.LastErrorText) Return End If System.DebugLog("HTTP response status: " + Str(resp.StatusCode)) Dim jsonResponse As New Chilkat.JsonObject success = jsonResponse.Load(resp.BodyStr) jsonResponse.EmitCompact = False System.DebugLog(jsonResponse.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 Dim date_gmt As New Chilkat.DtObj Dim href As String Dim embeddable As Boolean Dim name As String Dim templated As Boolean Dim id As Int32 id = jsonResponse.IntOf("id") Dim date As String date = jsonResponse.StringOf("date") success = jsonResponse.DtOf("date_gmt",False,date_gmt) Dim guidRendered As String guidRendered = jsonResponse.StringOf("guid.rendered") Dim guidRaw As String guidRaw = jsonResponse.StringOf("guid.raw") Dim modified As String modified = jsonResponse.StringOf("modified") Dim modified_gmt As String modified_gmt = jsonResponse.StringOf("modified_gmt") Dim slug As String slug = jsonResponse.StringOf("slug") Dim status As String status = jsonResponse.StringOf("status") Dim v_type As String v_type = jsonResponse.StringOf("type") Dim link As String link = jsonResponse.StringOf("link") Dim titleRaw As String titleRaw = jsonResponse.StringOf("title.raw") Dim titleRendered As String titleRendered = jsonResponse.StringOf("title.rendered") Dim author As Int32 author = jsonResponse.IntOf("author") Dim comment_status As String comment_status = jsonResponse.StringOf("comment_status") Dim ping_status As String ping_status = jsonResponse.StringOf("ping_status") Dim template As String template = jsonResponse.StringOf("template") Dim permalink_template As String permalink_template = jsonResponse.StringOf("permalink_template") Dim generated_slug As String generated_slug = jsonResponse.StringOf("generated_slug") Dim descriptionRaw As String descriptionRaw = jsonResponse.StringOf("description.raw") Dim descriptionRendered As String descriptionRendered = jsonResponse.StringOf("description.rendered") Dim captionRaw As String captionRaw = jsonResponse.StringOf("caption.raw") Dim captionRendered As String captionRendered = jsonResponse.StringOf("caption.rendered") Dim alt_text As String alt_text = jsonResponse.StringOf("alt_text") Dim media_type As String media_type = jsonResponse.StringOf("media_type") Dim mime_type As String mime_type = jsonResponse.StringOf("mime_type") Dim media_detailsWidth As Int32 media_detailsWidth = jsonResponse.IntOf("media_details.width") Dim media_detailsHeight As Int32 media_detailsHeight = jsonResponse.IntOf("media_details.height") Dim media_detailsFile As String media_detailsFile = jsonResponse.StringOf("media_details.file") Dim media_detailsSizesWoocommerce_gallery_thumbnailFile As String media_detailsSizesWoocommerce_gallery_thumbnailFile = jsonResponse.StringOf("media_details.sizes.woocommerce_gallery_thumbnail.file") Dim media_detailsSizesWoocommerce_gallery_thumbnailWidth As Int32 media_detailsSizesWoocommerce_gallery_thumbnailWidth = jsonResponse.IntOf("media_details.sizes.woocommerce_gallery_thumbnail.width") Dim media_detailsSizesWoocommerce_gallery_thumbnailHeight As Int32 media_detailsSizesWoocommerce_gallery_thumbnailHeight = jsonResponse.IntOf("media_details.sizes.woocommerce_gallery_thumbnail.height") Dim media_detailsSizesWoocommerce_gallery_thumbnailMime_type As String media_detailsSizesWoocommerce_gallery_thumbnailMime_type = jsonResponse.StringOf("media_details.sizes.woocommerce_gallery_thumbnail.mime_type") Dim media_detailsSizesWoocommerce_gallery_thumbnailSource_url As String media_detailsSizesWoocommerce_gallery_thumbnailSource_url = jsonResponse.StringOf("media_details.sizes.woocommerce_gallery_thumbnail.source_url") Dim media_detailsSizesShop_thumbnailFile As String media_detailsSizesShop_thumbnailFile = jsonResponse.StringOf("media_details.sizes.shop_thumbnail.file") Dim media_detailsSizesShop_thumbnailWidth As Int32 media_detailsSizesShop_thumbnailWidth = jsonResponse.IntOf("media_details.sizes.shop_thumbnail.width") Dim media_detailsSizesShop_thumbnailHeight As Int32 media_detailsSizesShop_thumbnailHeight = jsonResponse.IntOf("media_details.sizes.shop_thumbnail.height") Dim media_detailsSizesShop_thumbnailMime_type As String media_detailsSizesShop_thumbnailMime_type = jsonResponse.StringOf("media_details.sizes.shop_thumbnail.mime_type") Dim media_detailsSizesShop_thumbnailSource_url As String media_detailsSizesShop_thumbnailSource_url = jsonResponse.StringOf("media_details.sizes.shop_thumbnail.source_url") Dim media_detailsSizesFullFile As String media_detailsSizesFullFile = jsonResponse.StringOf("media_details.sizes.full.file") Dim media_detailsSizesFullWidth As Int32 media_detailsSizesFullWidth = jsonResponse.IntOf("media_details.sizes.full.width") Dim media_detailsSizesFullHeight As Int32 media_detailsSizesFullHeight = jsonResponse.IntOf("media_details.sizes.full.height") Dim media_detailsSizesFullMime_type As String media_detailsSizesFullMime_type = jsonResponse.StringOf("media_details.sizes.full.mime_type") Dim media_detailsSizesFullSource_url As String media_detailsSizesFullSource_url = jsonResponse.StringOf("media_details.sizes.full.source_url") Dim media_detailsImage_metaAperture As String media_detailsImage_metaAperture = jsonResponse.StringOf("media_details.image_meta.aperture") Dim media_detailsImage_metaCredit As String media_detailsImage_metaCredit = jsonResponse.StringOf("media_details.image_meta.credit") Dim media_detailsImage_metaCamera As String media_detailsImage_metaCamera = jsonResponse.StringOf("media_details.image_meta.camera") Dim media_detailsImage_metaCaption As String media_detailsImage_metaCaption = jsonResponse.StringOf("media_details.image_meta.caption") Dim media_detailsImage_metaCreated_timestamp As String media_detailsImage_metaCreated_timestamp = jsonResponse.StringOf("media_details.image_meta.created_timestamp") Dim media_detailsImage_metaCopyright As String media_detailsImage_metaCopyright = jsonResponse.StringOf("media_details.image_meta.copyright") Dim media_detailsImage_metaFocal_length As String media_detailsImage_metaFocal_length = jsonResponse.StringOf("media_details.image_meta.focal_length") Dim media_detailsImage_metaIso As String media_detailsImage_metaIso = jsonResponse.StringOf("media_details.image_meta.iso") Dim media_detailsImage_metaShutter_speed As String media_detailsImage_metaShutter_speed = jsonResponse.StringOf("media_details.image_meta.shutter_speed") Dim media_detailsImage_metaTitle As String media_detailsImage_metaTitle = jsonResponse.StringOf("media_details.image_meta.title") Dim media_detailsImage_metaOrientation As String media_detailsImage_metaOrientation = jsonResponse.StringOf("media_details.image_meta.orientation") Dim post As String post = jsonResponse.StringOf("post") Dim source_url As String source_url = jsonResponse.StringOf("source_url") Dim i As Int32 i = 0 Dim count_i As Int32 count_i = jsonResponse.SizeOfArray("meta") While i < count_i jsonResponse.I = i i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("media_details.image_meta.keywords") While i < count_i jsonResponse.I = i i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("missing_image_sizes") While i < count_i jsonResponse.I = i i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("_links.self") While i < count_i jsonResponse.I = i href = jsonResponse.StringOf("_links.self[i].href") i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("_links.collection") While i < count_i jsonResponse.I = i href = jsonResponse.StringOf("_links.collection[i].href") i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("_links.about") While i < count_i jsonResponse.I = i href = jsonResponse.StringOf("_links.about[i].href") i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("_links.author") While i < count_i jsonResponse.I = i embeddable = jsonResponse.BoolOf("_links.author[i].embeddable") href = jsonResponse.StringOf("_links.author[i].href") i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("_links.replies") While i < count_i jsonResponse.I = i embeddable = jsonResponse.BoolOf("_links.replies[i].embeddable") href = jsonResponse.StringOf("_links.replies[i].href") i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("_links.wp:action-unfiltered-html") While i < count_i jsonResponse.I = i href = jsonResponse.StringOf("_links.wp:action-unfiltered-html[i].href") i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("_links.wp:action-assign-author") While i < count_i jsonResponse.I = i href = jsonResponse.StringOf("_links.wp:action-assign-author[i].href") i = i + 1 Wend i = 0 count_i = jsonResponse.SizeOfArray("_links.curies") While i < count_i jsonResponse.I = i name = jsonResponse.StringOf("_links.curies[i].name") href = jsonResponse.StringOf("_links.curies[i].href") templated = jsonResponse.BoolOf("_links.curies[i].templated") i = i + 1 Wend |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.