0

I have this code here:

<?php 

$url = "http://otter.topsy.com/search.json?q=debt%20management&window=a";
$jsonfile = file_get_contents($url);
$obj = json_decode($jsonfile);

echo $obj->response[0]->list[0]->trackback_permalink;

?>

*Note: I have taken my API key out of the URL.

However, it returns this:

Notice: Trying to get property of non-object in C:\xampp\htdocs\topsyAPI\index.php on line 23

Notice: Trying to get property of non-object in C:\xampp\htdocs\topsyAPI\index.php on line 23

Notice: Trying to get property of non-object in C:\xampp\htdocs\topsyAPI\index.php on line 23

I tried using the true parameter on json_decode and it's still not working.

This is the first time i've worked with an API and JSON, so I am very much new to it.. Could someone help me out please?

14
  • 3
    can you post the output of var_dump($obj); Commented Nov 13, 2012 at 10:06
  • 5
    should be response, not response[0]. response isn't an array, its an object. Commented Nov 13, 2012 at 10:09
  • 1
    Thank you Shai Mishali! Done! Commented Nov 13, 2012 at 10:11
  • 1
    @Blake Is there a reason Svetlio was marked the right even though I answered earlier? :) Commented Nov 13, 2012 at 13:50
  • 1
    Just noticed you added it as an answer too, so marked it as right :) Commented Nov 13, 2012 at 17:12

2 Answers 2

2

As I wrote in the comments, response is an object, not an array. The reference should be to

$obj->response->list[0]->trackback_permalink

Not

$obj->response[0]->list[0]->trackback_permalink
Sign up to request clarification or add additional context in comments.

Comments

1
$obj->response->list[0]->trackback_permalink;

I think that your response is Object not array

5 Comments

There are things that are too small to have a full answer for :) He was answered and his problem was resolved, there's no need to be the first to write an answer just to get some points. Thats greedy.
It doesn't matter if they are small or large. Comment is comment and Answer is answer ..
Also there's no reason to downvote as this is a full answer, if you're not able to write it as a reply it's your own fault.
Fine, I think its rather childish - since you answered after his problem was already fixed, but whatever. cheers.
@ShaiMishali We are on StackOverflow PHP section, don't expect too much ;)

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.