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 )

12
  • 1
    Isn't that an assoc. array? Should be $data['id'] instead of $data[id] Commented Apr 28, 2012 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. Commented Apr 28, 2012 at 10:49
  • 1
    @Smamatti, yes, syntactically, id should be in quotes, but that's not what's causing the problem here. Commented Apr 28, 2012 at 10:49
  • and also equal sign <?= $data['id']; ?> Commented Apr 28, 2012 at 10:52
  • 2
    @jcubic, short_open_tag (<? ?>) is deprecated, and was removed in PHP 5.3.0 Commented Apr 28, 2012 at 10:58

2 Answers 2

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'];
?>
Sign up to request clarification or add additional context in comments.

5 Comments

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?
Ah, it's an array because you are using json_decode() which you did not mention in your original question.
@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!
the output is { (one curly brace) . whats happening?
sorry about that i forgot to remove the print_r. thanks NADH it worked
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

2 Comments

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, by default, json_decode() returns an object. If you want to get an array, try json_decode($data, true)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.