PHP JSON data foreach problem

Discussion in 'PHP' started by youlichika, Jan 15, 2011.

  1. #1
    I want to make a PHP JSON data foreach, but I met some problem.
    This is my php code:
    
    $ch = curl_init();//use $json = file_get_contents($url); it will return:file_get_contents() [function.file-get-contents]: SSL: fatal protocol error in ...
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    $body = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($body);//if add true, all the resalt is empty.
    foreach ($data as $result) { 
    echo '<div class="title"><a href="'.htmlspecialchars($result->link).'">'.htmlspecialchars($result->message).'<br />'.htmlspecialchars($result->description).'<br />'.htmlspecialchars($result->caption).'</a><br />';
    if(!empty($result->attachment->properties[0]->text)){
        foreach ($result->attachment->properties[0] as $properties) { 
    echo htmlspecialchars($properties->name).'<br /><a href="'.htmlspecialchars($properties->href).'">'.htmlspecialchars($properties->text).'</a></div>'; 
        }
    }
    if(!empty($result->attachment->media)){
    echo '<div class="image"><a href="'.htmlspecialchars($result->attachment->media[0]->href).'"><img src="'.htmlspecialchars($result->attachment->media[0]->src).'" /><br>'.htmlspecialchars($result->attachment->media[0]->type).'</a></div>'; 
    }
    }
    PHP:
    I make a var_dump and get the data return, I post it in the javascript part in http://jsfiddle.net/T375A/. remove outer string(47434)"" and copy to http://www.jsonlint.com/, can see a json tree clearly.

    First: I can not get the `properties` part. the return is empty.
    Second: it alawys show wrong in line: `echo '<div class="image">...` Fatal error: Cannot use object of type stdClass as array in ...

    How to foreach in a correct way? Thanks.
     
    youlichika, Jan 15, 2011 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you post var_dump($body) too?
     
    hogan_h, Jan 15, 2011 IP
  3. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    youlichika, Jan 16, 2011 IP
  4. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I already tested it like that and it seemed to work, now when you said it again i saw it works for the first 3-4 images, then it stops.

    Ok, the problem is that in your JSON data there are some empty properties and they are getting serialized as object with no properties and the empty check is failing on that (it always returns true, because in php object with no properties is not considered empty or null).

    Quick fix would be to use:

    if(is_array($result->attachment->properties))
    if(is_array($result->attachment->media))

    instead of is_empty(...).

    It doesn't look clean, but it works and for the moment i have no other idea, except maybe cleaning the JSON data on the emitting side (you could try using "properties":"" or "properties":null instead of "properties":{})
     
    hogan_h, Jan 16, 2011 IP
  5. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    @hogan_h, thanks, there is no error alart now.
    One more quesition: I add this code in the top of the page, but some data from json still like [​IMG]
    
    <?php header('Content-type:text/html; charset=utf-8'); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    Code (markup):
     
    youlichika, Jan 16, 2011 IP