Q:

How to get correct product url for multistore magento?

I am running following code in Magento root to get the product urls
<?php
require_once('app/Mage.php'); 
umask(0);
Mage::app(1);   

$collection = Mage::getModel('catalog/product')
            ->setStoreId(1)
            ->getCollection();
foreach( $collection as $product )
{
    echo $product->getProductUrl(); 
    echo "<br>";
}   

?>

I am getting product urls like http://example.com/catalog/product/view/id/5/ , But these urls are invalid.
The product urls are as following in front end http://example.com/product.html
How do I get the correct product urls? I have multi store Magento set-up.

A:

You need to get store url for each product separetly. In other words, you need to use something like this:
$collection = Mage::getModel('catalog/product')
            ->getCollection();

foreach( $collection as $product )
{
    echo $product->setStoreId(5)->getProductUrl();
    echo "<br>";
} 

