2

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 )

  • 1
    Isn't that an assoc. array? Should be `$data['id']` instead of `$data[id]` – Smamatti Apr 28 '12 at 10:48
  • If that is the array, then $data['id'] should work. Also, please use long open tags. Later down the road when you're stuck on a server that doesn't have short open tags enabled, you'll likely regret it. – Corbin Apr 28 '12 at 10:49
  • 1
    @Smamatti, yes, syntactically, `id` should be in quotes, but that's not what's causing the problem here. – Nadh Apr 28 '12 at 10:49
  • and also equal sign = $data['id']; ?> – jcubic Apr 28 '12 at 10:52
  • @NADH Alright, thanks! Wasn't sure about the return value, because it stated `mixed` in the API. Guess you are right then in your answer :-) – Smamatti Apr 28 '12 at 10:55
  • @NADH because he want to put it somewhere i html that why he use but is the same as or = json_encode($data)['id'] ?> but don't think that php allow to use index after a function call. Didn't read question carefully. – jcubic Apr 28 '12 at 10:55
  • 2
    @jcubic, `short_open_tag` (` ?>`) is deprecated, and was removed in PHP 5.3.0 – Nadh Apr 28 '12 at 10:58
  • @NADH Oh I din't know what about = $var ?> – jcubic Apr 28 '12 at 10:59
  • @NADH: I think (might be wrong) that jcubic meant that `=` is also a short open tag and should also not be used: http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – Stefan Apr 28 '12 at 10:59
  • @jcubic, `= ?>` is valid usage. However, it's only available in PHP 5.4.0+ and in versions before 5.3.0. – Nadh Apr 28 '12 at 11:03
  • @jcubic: It's only 'valid' if the server has short tags enabled. – Stefan Apr 28 '12 at 11:06
  • i edited the code. still cant access it. i just had problem posting thats why. its my first post – Roxy Gutera Apr 28 '12 at 11:09

2 Answers2

2

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'];
?>
Nadh
  • 6,987
  • 2
  • 21
  • 21
  • i used this print_r(json_decode($data,true)); the output is Array ( [id] => 112265748909490 [post_id] => 100003781972892_112203478915717 ) . then why is it an array? – Roxy Gutera Apr 28 '12 at 10:58
  • Ah, it's an array because you are using `json_decode()` which you did not mention in your original question. – Nadh Apr 28 '12 at 11:04
  • @RoxyGutera, I've edited my answer with code that works. While you are doing a `json_decode()` inside `print_r()`, you are not assigning it to the `$data` variable! – Nadh Apr 28 '12 at 11:10
  • the output is { (one curly brace) . whats happening? – Roxy Gutera Apr 28 '12 at 11:15
  • sorry about that i forgot to remove the print_r. thanks NADH it worked – Roxy Gutera Apr 28 '12 at 11:17
0

Are you accessing the Facebook Graph API?

If so just use:

$data = json_decode($data);

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

Diogo Raminhos
  • 2,023
  • 16
  • 24
  • that was my first approach but still $data->id is NULL. yes graph api and $data = json_decode($data); is in my code already. but still i cant access the array – Roxy Gutera Apr 28 '12 at 10:54
  • @Roxy, by default, `json_decode()` returns an object. If you want to get an array, try `json_decode($data, true)` – Nadh Apr 28 '12 at 10:59