Sample code for 30+ languages & platforms
Visual FoxPro

Download Photo to a File

See more Facebook Examples

Assuming we have the ID of a Photo, this example demonstrates how to download the photo image data to a file.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loOauth2
LOCAL loRest
LOCAL lcPhotoId
LOCAL loSbPath
LOCAL lcResponseJson
LOCAL loJson
LOCAL lcImageUrl
LOCAL loSbImageUrl
LOCAL loSbToPath
LOCAL lnBCaseSensitive
LOCAL loHttp

lnSuccess = 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
loOauth2 = CreateObject('Chilkat.OAuth2')
loOauth2.AccessToken = "FACEBOOK-ACCESS-TOKEN"

loRest = CreateObject('Chilkat.Rest')

* Connect to Facebook...
lnSuccess = loRest.Connect("graph.facebook.com",443,1,1)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loOauth2
    RELEASE loRest
    CANCEL
ENDIF

* Provide the authentication credentials (i.e. the access key)
loRest.SetAuthOAuth2(loOauth2)

* Assumes we've already obtained a Photo ID.
lcPhotoId = "10210199026347451"

loSbPath = CreateObject('Chilkat.StringBuilder')
loSbPath.Append("/v2.7/")
loSbPath.Append(lcPhotoId)

* First we're going to get the photo informaton so we can get the URL of the image file data.
* Select the fields we want.
* See https://developers.facebook.com/docs/graph-api/reference/photo/
loRest.AddQueryParam("fields","id,album,images")

lcResponseJson = loRest.FullRequestNoBody("GET",loSbPath.GetAsString())
IF (loRest.LastMethodSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loOauth2
    RELEASE loRest
    RELEASE loSbPath
    CANCEL
ENDIF

loJson = CreateObject('Chilkat.JsonObject')
loJson.EmitCompact = 0
loJson.Load(lcResponseJson)

* Show the JSON in human-readable format.
? loJson.Emit()

* Get the image URL.
lcImageUrl = loJson.StringOf("images[0].source")
? "Downloading from " + lcImageUrl

loSbImageUrl = CreateObject('Chilkat.StringBuilder')
loSbImageUrl.Append(lcImageUrl)

* Build the output local file path.
loSbToPath = CreateObject('Chilkat.StringBuilder')
loSbToPath.Append("qa_output/fb")
loSbToPath.Append(loJson.StringOf("id"))
lnBCaseSensitive = 0
IF (loSbImageUrl.Contains(".jpg",lnBCaseSensitive) = 1) THEN
    loSbToPath.Append(".jpg")
ELSE
    loSbToPath.Append(".png")
ENDIF

? "Downloading to " + loSbToPath.GetAsString()

* Download using Chilkat HTTP.
loHttp = CreateObject('Chilkat.Http')
lnSuccess = loHttp.Download(lcImageUrl,loSbToPath.GetAsString())
IF (lnSuccess <> 1) THEN
    ? loHttp.LastErrorText
ELSE
    ? "Downloaded."
ENDIF

RELEASE loOauth2
RELEASE loRest
RELEASE loSbPath
RELEASE loJson
RELEASE loSbImageUrl
RELEASE loSbToPath
RELEASE loHttp