Sample code for 30+ languages & platforms
PureBasic

Get Individual Photo Info

See more Facebook Examples

Assuming we have the ID of a Photo, this example demonstrates how to retrieve the photo information and parse the JSON.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkOAuth2.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; This example assumes a previously obtained an access token
    oauth2.i = CkOAuth2::ckCreate()
    If oauth2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkOAuth2::setCkAccessToken(oauth2, "FACEBOOK-ACCESS-TOKEN")

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

    ; Connect to Facebook...
    success = CkRest::ckConnect(rest,"graph.facebook.com",443,1,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkOAuth2::ckDispose(oauth2)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ; Provide the authentication credentials (i.e. the access key)
    CkRest::ckSetAuthOAuth2(rest,oauth2)

    ; Assumes we've already obtained a Photo ID.
    photoId.s = "10210199026347451"

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

    CkStringBuilder::ckAppend(sbPath,"/v2.7/")
    CkStringBuilder::ckAppend(sbPath,photoId)

    ; Select the fields we want.
    ; This example will select many of the possible fields.
    ; See https://developers.facebook.com/docs/graph-api/reference/photo/
    CkRest::ckAddQueryParam(rest,"fields","id,album,can_delete,can_tag,from,height,width,images,link,name,name_tags,picture,place,target")

    responseJson.s = CkRest::ckFullRequestNoBody(rest,"GET",CkStringBuilder::ckGetAsString(sbPath))
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkOAuth2::ckDispose(oauth2)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbPath)
        ProcedureReturn
    EndIf

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

    CkJsonObject::setCkEmitCompact(json, 0)
    CkJsonObject::ckLoad(json,responseJson)

    ; Show the JSON in human-readable format.
    Debug CkJsonObject::ckEmit(json)

    ; A sample response is shown below.
    ; Demonstrate how to parse values from the JSON.
    Debug "Album name: " + CkJsonObject::ckStringOf(json,"album.name")
    canDelete.i = CkJsonObject::ckBoolOf(json,"can_delete")
    Debug "Can Delete: " + Str(canDelete)
    Debug "From Name: " + CkJsonObject::ckStringOf(json,"from.name")
    height.i = CkJsonObject::ckIntOf(json,"height")
    width.i = CkJsonObject::ckIntOf(json,"width")
    Debug "Dimensions: " + Str(width) + "x" + Str(height)
    Debug "First Image Source: " + CkJsonObject::ckStringOf(json,"images[0].source")

    ; A sample JSON response is shown here.  
    ; { 
    ;   "id": "10210199026347451",
    ;   "album": { 
    ;     "created_time": "2009-10-19T00:06:46+0000",
    ;     "name": "Timeline Photos",
    ;     "id": "1237223526054"
    ;   },
    ;   "can_delete": true,
    ;   "can_tag": true,
    ;   "from": { 
    ;     "name": "Matt Smith",
    ;     "id": "10224048320139890"
    ;   },
    ;   "height": 120,
    ;   "width": 120,
    ;   "images": [
    ;     { 
    ;       "height": 120,
    ;       "source": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/14462791_10210199026347451_7830642117574407060_n.jpg?oh=a7f9ed10cf9cd81a82adeb541c60e2e2&oe=58ABB195",
    ;       "width": 120
    ;     }
    ;   ],
    ;   "link": "https:\/\/www.facebook.com\/photo.php?fbid=10210199026347451&set=a.1237223526054.2038240.1093202869&type=3",
    ;   "name": "Ignore my posts -- I'm doing some testing for Facebook related programming...",
    ;   "picture": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/14462791_10210199026347451_7830642117574407060_n.jpg?oh=a7f9ed10cf9cd81a82adeb541c60e2e2&oe=58ABB195"
    ; }
    ; 


    CkOAuth2::ckDispose(oauth2)
    CkRest::ckDispose(rest)
    CkStringBuilder::ckDispose(sbPath)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure