Need help PLEASE... (Array output)

Discussion in 'PHP' started by cgo85, Aug 24, 2008.

  1. #1
    Okay, I'm trying to use Yahoo pipes (output=PHP). I've used the code below to get what you can see here. I NEED to know how to turn that array information into this kind of format:

    Title
    Street
    City, State
    Phone

    <?php
    
    error_reporting(E_ALL);
    
    // output=php means that the request will return serialized PHP
    $request =  'http://pipes.yahoo.com/pipes/pipe.run?Location=98229&_id=ffa43b51699a926981d210f575d8ed6b&_render=php&subject=apartment';
    
    $response = file_get_contents($request);
    
    if ($response === false) {
    	die('Request failed');
    }
    
    $phpobj = unserialize($response);
    
    echo '<pre>';
    print_r($phpobj);
    echo '</pre>';
    
    ?>
    PHP:

    THANK YOU FOR YOUR HELP!
     
    cgo85, Aug 24, 2008 IP
  2. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    foreach ($phpobj['value']['items'] as $business) 
        $content .= "{$business['title']}<br />{$business['y:location']['street']}<br />{$business['y:location']['city']}, {$business['y:location']['state']}<br />{$business['phone']}<br /><br />";
    echo $content;
    Code (markup):
    I haven't tried it but that should work.
     
    rcadble, Aug 24, 2008 IP
    cgo85 likes this.
  3. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    rcadble... thanks, that pretty much works great! Only problem is I get this error message:

    Notice: Undefined variable: content in /home/chinedu8/public_html/pipes_local_api.php on line 18

    Here is the code again:

    <?php
    
    error_reporting(E_ALL);
    
    // output=php means that the request will return serialized PHP
    $request =  'http://pipes.yahoo.com/pipes/pipe.run?Location=98229&_id=ffa43b51699a926981d210f575d8ed6b&_render=php&subject=apartment';
    
    $response = file_get_contents($request);
    
    if ($response === false) {
    	die('Request failed');
    }
    
    $phpobj = unserialize($response);
    
    foreach ($phpobj['value']['items'] as $business) 
        $content .= "{$business['title']}<br />{$business['y:location']['street']}<br />{$business['y:location']['city']}, {$business['y:location']['state']}<br />{$business['Phone']}<br /><br />";
    echo $content;
    
    ?>
    PHP:
     
    cgo85, Aug 24, 2008 IP
  4. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's because of the strict errorreporting I think. Try this:

    <?php
    
    error_reporting(E_ALL);
    
    // output=php means that the request will return serialized PHP
    $request =  'http://pipes.yahoo.com/pipes/pipe.run?Location=98229&_id=ffa43b51699a926981d210f575d8ed6b&_render=php&subject=apartment';
    
    $response = file_get_contents($request);
    
    if ($response === false) {
        die('Request failed');
    }
    
    $phpobj = unserialize($response);
    
    $content = '';
    foreach ($phpobj['value']['items'] as $business)
        $content .= "{$business['title']}<br />{$business['y:location']['street']}<br />{$business['y:location']['city']}, {$business['y:location']['state']}<br />{$business['Phone']}<br /><br />";
    echo $content;
    
    ?>
    Code (markup):
     
    rcadble, Aug 24, 2008 IP
  5. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    cgo85, Aug 25, 2008 IP
  6. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm not sure about your second question, but you can limit the results.

    <?php
    
    error_reporting(E_ALL);
    
    // output=php means that the request will return serialized PHP
    $request =  'http://pipes.yahoo.com/pipes/pipe.run?Location=98229&_id=ffa43b51699a926981d210f575d8ed6b&_render=php&subject=apartment';
    
    $response = file_get_contents($request);
    
    if ($response === false) {
        die('Request failed');
    }
    
    $phpobj = unserialize($response);
    $limit = 20; //edit this to the limit
    $count = 0;
    foreach ($phpobj['value']['items'] as $business) {
        $content .= "{$business['title']}<br />{$business['y:location']['street']}<br />{$business['y:location']['city']}, {$business['y:location']['state']}<br />{$business['Phone']}<br /><br />";
        $count ++;
        if ($count >= $limit) 
            break;
    }
    echo $content;
    
    ?>
    Code (markup):
     
    rcadble, Aug 25, 2008 IP