0

I am using cURL to get json information from a site that pulls a random tumblr picture from a list of sources and I am interested of putting the json data retrieved into php variables so I can call for example, just the image url

$url = "http://someurl.com";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL,$url);

            $a = curl_exec($ch);
                    $json = var_dump(json_decode($a, true));

I get:

array(3) {
  'image' =>
  array(1) {
    [0] =>
    string(62) "http://picture.jpg"
  }
  'source' =>
  string(31) "http://source.tumblr.com"
  'page' =>
  string(9) "/page/164"
}

What would I now do in order to just print the url for the image?

I have tried

$url = $json["image"][0];

and then calling $url, but it gives me nothing in return. What I am doing wrong?

I have never worked with json before so I am at a loss here, any help is appreciated!

2
  • What happens if you just print out $json['image']? Commented May 19, 2014 at 13:33
  • 1
    try $json = json_decode($a, true); can you show full example you have Commented May 19, 2014 at 13:36

1 Answer 1

1

according to code looks:-

try change

$json = var_dump(json_decode($a, true));

to

$json = json_decode($a, true);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! That worked out just fine. If you don't mind, do explain why it works without var_dump. I am somewhat still of a newbie to php and I'd love to learn more.
@Chidorid if the answer is fine you first mark it accepted by clicking the tick on the left
you have seted your var_dump output in your $json var then how it will parse as array? got it or i need to describe more
@SaadChaudhry, There was apparently a 5 minute timer before I could mark it as answered, done now! =) RakeshSharma, Appears I was at a loss on how var_dump works, was using it to print the structure of the array. Thanks for the answer!

Your Answer

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