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

DataFlex 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
Cloud Signature CSC
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
Secrets
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

 

 

 

(DataFlex) 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 ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Variant vReq
    Handle hoReq
    Boolean iSuccess
    Variant vResp
    Handle hoResp
    Handle hoJsonResponse
    Variant vDate_gmt
    Handle hoDate_gmt
    String sHref
    Boolean iEmbeddable
    String sName
    Boolean iTemplated
    Integer iId
    String sDate
    String sGuidRendered
    String sGuidRaw
    String sModified
    String sModified_gmt
    String sSlug
    String sStatus
    String sV_type
    String sLink
    String sTitleRaw
    String sTitleRendered
    Integer iAuthor
    String sComment_status
    String sPing_status
    String sTemplate
    String sPermalink_template
    String sGenerated_slug
    String sDescriptionRaw
    String sDescriptionRendered
    String sCaptionRaw
    String sCaptionRendered
    String sAlt_text
    String sMedia_type
    String sMime_type
    Integer iMedia_detailsWidth
    Integer iMedia_detailsHeight
    String sMedia_detailsFile
    String sMedia_detailsSizesWoocommerce_gallery_thumbnailFile
    Integer iMedia_detailsSizesWoocommerce_gallery_thumbnailWidth
    Integer iMedia_detailsSizesWoocommerce_gallery_thumbnailHeight
    String sMedia_detailsSizesWoocommerce_gallery_thumbnailMime_type
    String sMedia_detailsSizesWoocommerce_gallery_thumbnailSource_url
    String sMedia_detailsSizesShop_thumbnailFile
    Integer iMedia_detailsSizesShop_thumbnailWidth
    Integer iMedia_detailsSizesShop_thumbnailHeight
    String sMedia_detailsSizesShop_thumbnailMime_type
    String sMedia_detailsSizesShop_thumbnailSource_url
    String sMedia_detailsSizesFullFile
    Integer iMedia_detailsSizesFullWidth
    Integer iMedia_detailsSizesFullHeight
    String sMedia_detailsSizesFullMime_type
    String sMedia_detailsSizesFullSource_url
    String sMedia_detailsImage_metaAperture
    String sMedia_detailsImage_metaCredit
    String sMedia_detailsImage_metaCamera
    String sMedia_detailsImage_metaCaption
    String sMedia_detailsImage_metaCreated_timestamp
    String sMedia_detailsImage_metaCopyright
    String sMedia_detailsImage_metaFocal_length
    String sMedia_detailsImage_metaIso
    String sMedia_detailsImage_metaShutter_speed
    String sMedia_detailsImage_metaTitle
    String sMedia_detailsImage_metaOrientation
    String sPost
    String sSource_url
    Integer i
    Integer iCount_i
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // 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.
    Set ComLogin Of hoHttp To "wp_username"
    // Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
    Set ComPassword Of hoHttp To "app_password"
    Set ComBasicAuth Of hoHttp To True

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Set ComHttpVerb Of hoReq To "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 

    Set ComContentType Of hoReq To "image/jpg"
    Set ComPath Of hoReq To "/wp-json/wp/v2/media"

    Get ComLoadBodyFromFile Of hoReq "qa_data/jpg/starfish.jpg" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReq To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Send ComAddHeader To hoReq "Content-Disposition" "attachment; filename=starfish.jpg"

    Send ComAddHeader To hoReq "Expect" "100-continue"

    Get pvComObject of hoReq to vReq
    Get ComSynchronousRequest Of hoHttp "www.yourserver.com" 443 True vReq To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln "HTTP response status: " iTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResponse
    If (Not(IsComObjectCreated(hoJsonResponse))) Begin
        Send CreateComObject of hoJsonResponse
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoJsonResponse sTemp1 To iSuccess
    Send Destroy of hoResp

    Set ComEmitCompact Of hoJsonResponse To False
    Get ComEmit Of hoJsonResponse To sTemp1
    Showln sTemp1

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

    Get Create (RefClass(cComChilkatDtObj)) To hoDate_gmt
    If (Not(IsComObjectCreated(hoDate_gmt))) Begin
        Send CreateComObject of hoDate_gmt
    End

    Get ComIntOf Of hoJsonResponse "id" To iId
    Get ComStringOf Of hoJsonResponse "date" To sDate
    Get pvComObject of hoDate_gmt to vDate_gmt
    Get ComDtOf Of hoJsonResponse "date_gmt" False vDate_gmt To iSuccess
    Get ComStringOf Of hoJsonResponse "guid.rendered" To sGuidRendered
    Get ComStringOf Of hoJsonResponse "guid.raw" To sGuidRaw
    Get ComStringOf Of hoJsonResponse "modified" To sModified
    Get ComStringOf Of hoJsonResponse "modified_gmt" To sModified_gmt
    Get ComStringOf Of hoJsonResponse "slug" To sSlug
    Get ComStringOf Of hoJsonResponse "status" To sStatus
    Get ComStringOf Of hoJsonResponse "type" To sV_type
    Get ComStringOf Of hoJsonResponse "link" To sLink
    Get ComStringOf Of hoJsonResponse "title.raw" To sTitleRaw
    Get ComStringOf Of hoJsonResponse "title.rendered" To sTitleRendered
    Get ComIntOf Of hoJsonResponse "author" To iAuthor
    Get ComStringOf Of hoJsonResponse "comment_status" To sComment_status
    Get ComStringOf Of hoJsonResponse "ping_status" To sPing_status
    Get ComStringOf Of hoJsonResponse "template" To sTemplate
    Get ComStringOf Of hoJsonResponse "permalink_template" To sPermalink_template
    Get ComStringOf Of hoJsonResponse "generated_slug" To sGenerated_slug
    Get ComStringOf Of hoJsonResponse "description.raw" To sDescriptionRaw
    Get ComStringOf Of hoJsonResponse "description.rendered" To sDescriptionRendered
    Get ComStringOf Of hoJsonResponse "caption.raw" To sCaptionRaw
    Get ComStringOf Of hoJsonResponse "caption.rendered" To sCaptionRendered
    Get ComStringOf Of hoJsonResponse "alt_text" To sAlt_text
    Get ComStringOf Of hoJsonResponse "media_type" To sMedia_type
    Get ComStringOf Of hoJsonResponse "mime_type" To sMime_type
    Get ComIntOf Of hoJsonResponse "media_details.width" To iMedia_detailsWidth
    Get ComIntOf Of hoJsonResponse "media_details.height" To iMedia_detailsHeight
    Get ComStringOf Of hoJsonResponse "media_details.file" To sMedia_detailsFile
    Get ComStringOf Of hoJsonResponse "media_details.sizes.woocommerce_gallery_thumbnail.file" To sMedia_detailsSizesWoocommerce_gallery_thumbnailFile
    Get ComIntOf Of hoJsonResponse "media_details.sizes.woocommerce_gallery_thumbnail.width" To iMedia_detailsSizesWoocommerce_gallery_thumbnailWidth
    Get ComIntOf Of hoJsonResponse "media_details.sizes.woocommerce_gallery_thumbnail.height" To iMedia_detailsSizesWoocommerce_gallery_thumbnailHeight
    Get ComStringOf Of hoJsonResponse "media_details.sizes.woocommerce_gallery_thumbnail.mime_type" To sMedia_detailsSizesWoocommerce_gallery_thumbnailMime_type
    Get ComStringOf Of hoJsonResponse "media_details.sizes.woocommerce_gallery_thumbnail.source_url" To sMedia_detailsSizesWoocommerce_gallery_thumbnailSource_url
    Get ComStringOf Of hoJsonResponse "media_details.sizes.shop_thumbnail.file" To sMedia_detailsSizesShop_thumbnailFile
    Get ComIntOf Of hoJsonResponse "media_details.sizes.shop_thumbnail.width" To iMedia_detailsSizesShop_thumbnailWidth
    Get ComIntOf Of hoJsonResponse "media_details.sizes.shop_thumbnail.height" To iMedia_detailsSizesShop_thumbnailHeight
    Get ComStringOf Of hoJsonResponse "media_details.sizes.shop_thumbnail.mime_type" To sMedia_detailsSizesShop_thumbnailMime_type
    Get ComStringOf Of hoJsonResponse "media_details.sizes.shop_thumbnail.source_url" To sMedia_detailsSizesShop_thumbnailSource_url
    Get ComStringOf Of hoJsonResponse "media_details.sizes.full.file" To sMedia_detailsSizesFullFile
    Get ComIntOf Of hoJsonResponse "media_details.sizes.full.width" To iMedia_detailsSizesFullWidth
    Get ComIntOf Of hoJsonResponse "media_details.sizes.full.height" To iMedia_detailsSizesFullHeight
    Get ComStringOf Of hoJsonResponse "media_details.sizes.full.mime_type" To sMedia_detailsSizesFullMime_type
    Get ComStringOf Of hoJsonResponse "media_details.sizes.full.source_url" To sMedia_detailsSizesFullSource_url
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.aperture" To sMedia_detailsImage_metaAperture
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.credit" To sMedia_detailsImage_metaCredit
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.camera" To sMedia_detailsImage_metaCamera
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.caption" To sMedia_detailsImage_metaCaption
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.created_timestamp" To sMedia_detailsImage_metaCreated_timestamp
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.copyright" To sMedia_detailsImage_metaCopyright
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.focal_length" To sMedia_detailsImage_metaFocal_length
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.iso" To sMedia_detailsImage_metaIso
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.shutter_speed" To sMedia_detailsImage_metaShutter_speed
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.title" To sMedia_detailsImage_metaTitle
    Get ComStringOf Of hoJsonResponse "media_details.image_meta.orientation" To sMedia_detailsImage_metaOrientation
    Get ComStringOf Of hoJsonResponse "post" To sPost
    Get ComStringOf Of hoJsonResponse "source_url" To sSource_url
    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "meta" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "media_details.image_meta.keywords" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "missing_image_sizes" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "_links.self" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComStringOf Of hoJsonResponse "_links.self[i].href" To sHref
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "_links.collection" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComStringOf Of hoJsonResponse "_links.collection[i].href" To sHref
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "_links.about" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComStringOf Of hoJsonResponse "_links.about[i].href" To sHref
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "_links.author" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComBoolOf Of hoJsonResponse "_links.author[i].embeddable" To iEmbeddable
        Get ComStringOf Of hoJsonResponse "_links.author[i].href" To sHref
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "_links.replies" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComBoolOf Of hoJsonResponse "_links.replies[i].embeddable" To iEmbeddable
        Get ComStringOf Of hoJsonResponse "_links.replies[i].href" To sHref
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "_links.wp:action-unfiltered-html" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComStringOf Of hoJsonResponse "_links.wp:action-unfiltered-html[i].href" To sHref
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "_links.wp:action-assign-author" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComStringOf Of hoJsonResponse "_links.wp:action-assign-author[i].href" To sHref
        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "_links.curies" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComStringOf Of hoJsonResponse "_links.curies[i].name" To sName
        Get ComStringOf Of hoJsonResponse "_links.curies[i].href" To sHref
        Get ComBoolOf Of hoJsonResponse "_links.curies[i].templated" To iTemplated
        Move (i + 1) To i
    Loop



End_Procedure

 

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