Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Swift 3,4,5... Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Swift 3,4,5...) WordPress Media Upload

See more WordPress Examples

Demonstrates how to upload a media file to WordPress.

For more information, see https://developer.wordpress.org/rest-api/reference/media/#create-a-media-item

Chilkat Downloads for the Swift Programming Language

MAC OS X (Cocoa) Objective-C/Swift Libs

iOS Objective-C/Swift Libs

func chilkatTest() {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    // 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

    let req = CkoHttpRequest()!
    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"

    var success: Bool = req.loadBody(fromFile: "qa_data/jpg/starfish.jpg")
    if success == false {
        print("\(req.lastErrorText!)")
        return
    }

    req.addHeader("Content-Disposition", value: "attachment; filename=starfish.jpg")

    req.addHeader("Expect", value: "100-continue")

    var resp: CkoHttpResponse? = http.synchronousRequest("www.yourserver.com", port: 443, ssl: true, req: req)
    if http.lastMethodSuccess != true {
        print("\(http.lastErrorText!)")
        return
    }

    print("HTTP response status: \(resp!.statusCode.intValue)")

    let jsonResponse = CkoJsonObject()!
    jsonResponse.load(resp!.bodyStr)
    resp = nil

    jsonResponse.emitCompact = false
    print("\(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

    let date_gmt = CkoDtObj()!
    var href: String?
    var embeddable: Bool
    var name: String?
    var templated: Bool

    var id: Int = jsonResponse.int(of: "id").intValue
    var date: String? = jsonResponse.string(of: "date")
    jsonResponse.dt(of: "date_gmt", bLocal: false, dt: date_gmt)
    var guidRendered: String? = jsonResponse.string(of: "guid.rendered")
    var guidRaw: String? = jsonResponse.string(of: "guid.raw")
    var modified: String? = jsonResponse.string(of: "modified")
    var modified_gmt: String? = jsonResponse.string(of: "modified_gmt")
    var slug: String? = jsonResponse.string(of: "slug")
    var status: String? = jsonResponse.string(of: "status")
    var v_type: String? = jsonResponse.string(of: "type")
    var link: String? = jsonResponse.string(of: "link")
    var titleRaw: String? = jsonResponse.string(of: "title.raw")
    var titleRendered: String? = jsonResponse.string(of: "title.rendered")
    var author: Int = jsonResponse.int(of: "author").intValue
    var comment_status: String? = jsonResponse.string(of: "comment_status")
    var ping_status: String? = jsonResponse.string(of: "ping_status")
    var template: String? = jsonResponse.string(of: "template")
    var permalink_template: String? = jsonResponse.string(of: "permalink_template")
    var generated_slug: String? = jsonResponse.string(of: "generated_slug")
    var descriptionRaw: String? = jsonResponse.string(of: "description.raw")
    var descriptionRendered: String? = jsonResponse.string(of: "description.rendered")
    var captionRaw: String? = jsonResponse.string(of: "caption.raw")
    var captionRendered: String? = jsonResponse.string(of: "caption.rendered")
    var alt_text: String? = jsonResponse.string(of: "alt_text")
    var media_type: String? = jsonResponse.string(of: "media_type")
    var mime_type: String? = jsonResponse.string(of: "mime_type")
    var media_detailsWidth: Int = jsonResponse.int(of: "media_details.width").intValue
    var media_detailsHeight: Int = jsonResponse.int(of: "media_details.height").intValue
    var media_detailsFile: String? = jsonResponse.string(of: "media_details.file")
    var media_detailsSizesWoocommerce_gallery_thumbnailFile: String? = jsonResponse.string(of: "media_details.sizes.woocommerce_gallery_thumbnail.file")
    var media_detailsSizesWoocommerce_gallery_thumbnailWidth: Int = jsonResponse.int(of: "media_details.sizes.woocommerce_gallery_thumbnail.width").intValue
    var media_detailsSizesWoocommerce_gallery_thumbnailHeight: Int = jsonResponse.int(of: "media_details.sizes.woocommerce_gallery_thumbnail.height").intValue
    var media_detailsSizesWoocommerce_gallery_thumbnailMime_type: String? = jsonResponse.string(of: "media_details.sizes.woocommerce_gallery_thumbnail.mime_type")
    var media_detailsSizesWoocommerce_gallery_thumbnailSource_url: String? = jsonResponse.string(of: "media_details.sizes.woocommerce_gallery_thumbnail.source_url")
    var media_detailsSizesShop_thumbnailFile: String? = jsonResponse.string(of: "media_details.sizes.shop_thumbnail.file")
    var media_detailsSizesShop_thumbnailWidth: Int = jsonResponse.int(of: "media_details.sizes.shop_thumbnail.width").intValue
    var media_detailsSizesShop_thumbnailHeight: Int = jsonResponse.int(of: "media_details.sizes.shop_thumbnail.height").intValue
    var media_detailsSizesShop_thumbnailMime_type: String? = jsonResponse.string(of: "media_details.sizes.shop_thumbnail.mime_type")
    var media_detailsSizesShop_thumbnailSource_url: String? = jsonResponse.string(of: "media_details.sizes.shop_thumbnail.source_url")
    var media_detailsSizesFullFile: String? = jsonResponse.string(of: "media_details.sizes.full.file")
    var media_detailsSizesFullWidth: Int = jsonResponse.int(of: "media_details.sizes.full.width").intValue
    var media_detailsSizesFullHeight: Int = jsonResponse.int(of: "media_details.sizes.full.height").intValue
    var media_detailsSizesFullMime_type: String? = jsonResponse.string(of: "media_details.sizes.full.mime_type")
    var media_detailsSizesFullSource_url: String? = jsonResponse.string(of: "media_details.sizes.full.source_url")
    var media_detailsImage_metaAperture: String? = jsonResponse.string(of: "media_details.image_meta.aperture")
    var media_detailsImage_metaCredit: String? = jsonResponse.string(of: "media_details.image_meta.credit")
    var media_detailsImage_metaCamera: String? = jsonResponse.string(of: "media_details.image_meta.camera")
    var media_detailsImage_metaCaption: String? = jsonResponse.string(of: "media_details.image_meta.caption")
    var media_detailsImage_metaCreated_timestamp: String? = jsonResponse.string(of: "media_details.image_meta.created_timestamp")
    var media_detailsImage_metaCopyright: String? = jsonResponse.string(of: "media_details.image_meta.copyright")
    var media_detailsImage_metaFocal_length: String? = jsonResponse.string(of: "media_details.image_meta.focal_length")
    var media_detailsImage_metaIso: String? = jsonResponse.string(of: "media_details.image_meta.iso")
    var media_detailsImage_metaShutter_speed: String? = jsonResponse.string(of: "media_details.image_meta.shutter_speed")
    var media_detailsImage_metaTitle: String? = jsonResponse.string(of: "media_details.image_meta.title")
    var media_detailsImage_metaOrientation: String? = jsonResponse.string(of: "media_details.image_meta.orientation")
    var post: String? = jsonResponse.string(of: "post")
    var source_url: String? = jsonResponse.string(of: "source_url")
    var i: Int = 0
    var count_i: Int = jsonResponse.size(ofArray: "meta").intValue
    while i < count_i {
        jsonResponse.i = i
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "media_details.image_meta.keywords").intValue
    while i < count_i {
        jsonResponse.i = i
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "missing_image_sizes").intValue
    while i < count_i {
        jsonResponse.i = i
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "_links.self").intValue
    while i < count_i {
        jsonResponse.i = i
        href = jsonResponse.string(of: "_links.self[i].href")
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "_links.collection").intValue
    while i < count_i {
        jsonResponse.i = i
        href = jsonResponse.string(of: "_links.collection[i].href")
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "_links.about").intValue
    while i < count_i {
        jsonResponse.i = i
        href = jsonResponse.string(of: "_links.about[i].href")
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "_links.author").intValue
    while i < count_i {
        jsonResponse.i = i
        embeddable = jsonResponse.bool(of: "_links.author[i].embeddable")
        href = jsonResponse.string(of: "_links.author[i].href")
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "_links.replies").intValue
    while i < count_i {
        jsonResponse.i = i
        embeddable = jsonResponse.bool(of: "_links.replies[i].embeddable")
        href = jsonResponse.string(of: "_links.replies[i].href")
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "_links.wp:action-unfiltered-html").intValue
    while i < count_i {
        jsonResponse.i = i
        href = jsonResponse.string(of: "_links.wp:action-unfiltered-html[i].href")
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "_links.wp:action-assign-author").intValue
    while i < count_i {
        jsonResponse.i = i
        href = jsonResponse.string(of: "_links.wp:action-assign-author[i].href")
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.size(ofArray: "_links.curies").intValue
    while i < count_i {
        jsonResponse.i = i
        name = jsonResponse.string(of: "_links.curies[i].name")
        href = jsonResponse.string(of: "_links.curies[i].href")
        templated = jsonResponse.bool(of: "_links.curies[i].templated")
        i = i + 1
    }


}

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.