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

PureBasic 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

 

 

 

(PureBasic) 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 PureBasic Module Download

Chilkat PureBasic Module

IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpRequest.pb"
IncludeFile "CkDtObj.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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.
    CkHttp::setCkLogin(http, "wp_username")
    ; Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
    CkHttp::setCkPassword(http, "app_password")
    CkHttp::setCkBasicAuth(http, 1)

    req.i = CkHttpRequest::ckCreate()
    If req.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpRequest::setCkHttpVerb(req, "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 

    CkHttpRequest::setCkContentType(req, "image/jpg")
    CkHttpRequest::setCkPath(req, "/wp-json/wp/v2/media")

    success.i = CkHttpRequest::ckLoadBodyFromFile(req,"qa_data/jpg/starfish.jpg")
    If success = 0
        Debug CkHttpRequest::ckLastErrorText(req)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        ProcedureReturn
    EndIf

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

    CkHttpRequest::ckAddHeader(req,"Expect","100-continue")

    resp.i = CkHttp::ckSynchronousRequest(http,"www.yourserver.com",443,1,req)
    If CkHttp::ckLastMethodSuccess(http) <> 1
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        ProcedureReturn
    EndIf

    Debug "HTTP response status: " + Str(CkHttpResponse::ckStatusCode(resp))

    jsonResponse.i = CkJsonObject::ckCreate()
    If jsonResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(jsonResponse,CkHttpResponse::ckBodyStr(resp))
    CkHttpResponse::ckDispose(resp)

    CkJsonObject::setCkEmitCompact(jsonResponse, 0)
    Debug CkJsonObject::ckEmit(jsonResponse)

    ; 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

    date_gmt.i = CkDtObj::ckCreate()
    If date_gmt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    href.s
    embeddable.i
    name.s
    templated.i

    id.i = CkJsonObject::ckIntOf(jsonResponse,"id")
    date.s = CkJsonObject::ckStringOf(jsonResponse,"date")
    CkJsonObject::ckDtOf(jsonResponse,"date_gmt",0,date_gmt)
    guidRendered.s = CkJsonObject::ckStringOf(jsonResponse,"guid.rendered")
    guidRaw.s = CkJsonObject::ckStringOf(jsonResponse,"guid.raw")
    modified.s = CkJsonObject::ckStringOf(jsonResponse,"modified")
    modified_gmt.s = CkJsonObject::ckStringOf(jsonResponse,"modified_gmt")
    slug.s = CkJsonObject::ckStringOf(jsonResponse,"slug")
    status.s = CkJsonObject::ckStringOf(jsonResponse,"status")
    v_type.s = CkJsonObject::ckStringOf(jsonResponse,"type")
    link.s = CkJsonObject::ckStringOf(jsonResponse,"link")
    titleRaw.s = CkJsonObject::ckStringOf(jsonResponse,"title.raw")
    titleRendered.s = CkJsonObject::ckStringOf(jsonResponse,"title.rendered")
    author.i = CkJsonObject::ckIntOf(jsonResponse,"author")
    comment_status.s = CkJsonObject::ckStringOf(jsonResponse,"comment_status")
    ping_status.s = CkJsonObject::ckStringOf(jsonResponse,"ping_status")
    template.s = CkJsonObject::ckStringOf(jsonResponse,"template")
    permalink_template.s = CkJsonObject::ckStringOf(jsonResponse,"permalink_template")
    generated_slug.s = CkJsonObject::ckStringOf(jsonResponse,"generated_slug")
    descriptionRaw.s = CkJsonObject::ckStringOf(jsonResponse,"description.raw")
    descriptionRendered.s = CkJsonObject::ckStringOf(jsonResponse,"description.rendered")
    captionRaw.s = CkJsonObject::ckStringOf(jsonResponse,"caption.raw")
    captionRendered.s = CkJsonObject::ckStringOf(jsonResponse,"caption.rendered")
    alt_text.s = CkJsonObject::ckStringOf(jsonResponse,"alt_text")
    media_type.s = CkJsonObject::ckStringOf(jsonResponse,"media_type")
    mime_type.s = CkJsonObject::ckStringOf(jsonResponse,"mime_type")
    media_detailsWidth.i = CkJsonObject::ckIntOf(jsonResponse,"media_details.width")
    media_detailsHeight.i = CkJsonObject::ckIntOf(jsonResponse,"media_details.height")
    media_detailsFile.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.file")
    media_detailsSizesWoocommerce_gallery_thumbnailFile.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.file")
    media_detailsSizesWoocommerce_gallery_thumbnailWidth.i = CkJsonObject::ckIntOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.width")
    media_detailsSizesWoocommerce_gallery_thumbnailHeight.i = CkJsonObject::ckIntOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.height")
    media_detailsSizesWoocommerce_gallery_thumbnailMime_type.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.mime_type")
    media_detailsSizesWoocommerce_gallery_thumbnailSource_url.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.sizes.woocommerce_gallery_thumbnail.source_url")
    media_detailsSizesShop_thumbnailFile.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.sizes.shop_thumbnail.file")
    media_detailsSizesShop_thumbnailWidth.i = CkJsonObject::ckIntOf(jsonResponse,"media_details.sizes.shop_thumbnail.width")
    media_detailsSizesShop_thumbnailHeight.i = CkJsonObject::ckIntOf(jsonResponse,"media_details.sizes.shop_thumbnail.height")
    media_detailsSizesShop_thumbnailMime_type.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.sizes.shop_thumbnail.mime_type")
    media_detailsSizesShop_thumbnailSource_url.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.sizes.shop_thumbnail.source_url")
    media_detailsSizesFullFile.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.sizes.full.file")
    media_detailsSizesFullWidth.i = CkJsonObject::ckIntOf(jsonResponse,"media_details.sizes.full.width")
    media_detailsSizesFullHeight.i = CkJsonObject::ckIntOf(jsonResponse,"media_details.sizes.full.height")
    media_detailsSizesFullMime_type.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.sizes.full.mime_type")
    media_detailsSizesFullSource_url.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.sizes.full.source_url")
    media_detailsImage_metaAperture.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.aperture")
    media_detailsImage_metaCredit.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.credit")
    media_detailsImage_metaCamera.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.camera")
    media_detailsImage_metaCaption.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.caption")
    media_detailsImage_metaCreated_timestamp.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.created_timestamp")
    media_detailsImage_metaCopyright.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.copyright")
    media_detailsImage_metaFocal_length.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.focal_length")
    media_detailsImage_metaIso.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.iso")
    media_detailsImage_metaShutter_speed.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.shutter_speed")
    media_detailsImage_metaTitle.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.title")
    media_detailsImage_metaOrientation.s = CkJsonObject::ckStringOf(jsonResponse,"media_details.image_meta.orientation")
    post.s = CkJsonObject::ckStringOf(jsonResponse,"post")
    source_url.s = CkJsonObject::ckStringOf(jsonResponse,"source_url")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jsonResponse,"meta")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"media_details.image_meta.keywords")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"missing_image_sizes")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"_links.self")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        href = CkJsonObject::ckStringOf(jsonResponse,"_links.self[i].href")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"_links.collection")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        href = CkJsonObject::ckStringOf(jsonResponse,"_links.collection[i].href")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"_links.about")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        href = CkJsonObject::ckStringOf(jsonResponse,"_links.about[i].href")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"_links.author")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        embeddable = CkJsonObject::ckBoolOf(jsonResponse,"_links.author[i].embeddable")
        href = CkJsonObject::ckStringOf(jsonResponse,"_links.author[i].href")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"_links.replies")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        embeddable = CkJsonObject::ckBoolOf(jsonResponse,"_links.replies[i].embeddable")
        href = CkJsonObject::ckStringOf(jsonResponse,"_links.replies[i].href")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"_links.wp:action-unfiltered-html")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        href = CkJsonObject::ckStringOf(jsonResponse,"_links.wp:action-unfiltered-html[i].href")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"_links.wp:action-assign-author")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        href = CkJsonObject::ckStringOf(jsonResponse,"_links.wp:action-assign-author[i].href")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"_links.curies")
    While i < count_i
        CkJsonObject::setCkI(jsonResponse, i)
        name = CkJsonObject::ckStringOf(jsonResponse,"_links.curies[i].name")
        href = CkJsonObject::ckStringOf(jsonResponse,"_links.curies[i].href")
        templated = CkJsonObject::ckBoolOf(jsonResponse,"_links.curies[i].templated")
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkHttpRequest::ckDispose(req)
    CkJsonObject::ckDispose(jsonResponse)
    CkDtObj::ckDispose(date_gmt)


    ProcedureReturn
EndProcedure

 

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