Roxy Gutera
Roxy Gutera

Reputation: 23

Acessing The Return Array

edit:

<?php
    $file= 'fbplus.jpg';

    $args = array(
       'message' => $album_message,
    );
    $args[basename($file)] = '@' . realpath($file);
    $ch = curl_init();
    $url = $graph_url;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    $data = curl_exec($ch);
    //returns the photo id
    print_r(json_decode($data,true));


    echo "$data['id'] "; //wont work
        echo "$data[id] "; //wont work


?>

this was the return, after i successfully uploaded a photo.

Array ( [id] => 112235735579158 [post_id] => 100003781972892_112203478915717 )

Upvotes: 2

Views: 90

Answers (2)

Nadh
Nadh

Reputation: 7243

curl_exec() returns a string (contents of the webpage), not an Array. So you cannot do $data['id'] because $data is a string and not an array.

What is the url you are posting to? What is its exact output?

EDIT: Looks like the url returns JSON. In that case:

<?php
    $file= 'fbplus.jpg';

    $args = array(
       'message' => $album_message,
    );
    $args[basename($file)] = '@' . realpath($file);
    $ch = curl_init();
    $url = $graph_url;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    $data = curl_exec($ch);
    //returns the photo id

    $data = json_decode($data,true);

    echo $data['id'];
?>

Upvotes: 2

Diogo Raminhos
Diogo Raminhos

Reputation: 2023

Are you accessing the Facebook Graph API?

If so just use:

$data = json_decode($data);

and the you can access it trough $data->id

Upvotes: 0

Related Questions