开发者

PHP/HTML/mySQL URL Caching Problem

I have this function: http://past开发者_如何学Goebin.ca/2058418

It basically checks to see if a tables contains some URLs to pictures of a band. If it does, then it will order the table by random, choose the first result and then output the html code to insert the picture. If the table does not have the pictures of that specific band, then it downloads an XML file which contains the image URL's, parses the XML and inserts in into the table, and then gets the HTML code for the image like before.

In terms out html output, you can't tell if the image URL has been cached or not. HOWEVER, when the image URL is cached (for the first time), whatever web browser you use will not display the image. The HTML is fine - the image is linked correctly.

Do you have any ideas? A live version of the site which contains this function is here: http://redfern.me/similar/. I have just emptied the tables, so there shouldn't be many cached URL's. Try choosing a band, and then see if the image loads. You can tell if the URL's where cached or not by looking at the bottom of the page.


basically looks like you wasn't returning after you fetched the image first time.

 <?php function getimage($artist){
        $api_key = "XXXXXXXXX";
        $iquery = mysql_query("SELECT url FROM `images` WHERE artist = '".$artist."' ORDER BY RAND() LIMIT 1");
        if($artist != ""){
            $artist = str_replace(" ", "+", $artist);
            if(mysql_num_rows($iquery) == 0){
                $url = "http://developer.echonest.com/api/v4/artist/images?format=xml&api_key=".$api_key."&name=".$artist."&results=20";
                $data = file_get_contents($url);
                if($data=false){return 'Error Getting Image';}

                $images = new SimpleXMLElement($data);

                foreach($images as $image){
                    foreach($image->image as $indimage){
                        $insiquery = "INSERT INTO images (id, artist, url) VALUES (NULL, '$artist','".$indimage->url."')";
                        mysql_query($insiquery);
                    }
                }
                return "<img src=\"".$indimage->url."\" alt=\"$artist image\" />";
            }else{
                $imgurl = mysql_fetch_array($iquery);
                return"<img src=\"".$imgurl['url']."\" alt=\"$artist image\" />";
            }

        }
        else{
            return"Image Aquire Function Error: <i>No Band Specified</i>";
        }
        return null;
    }?>


echo getimage('Britney Spears');


That's simply because when you enter to the "xml call" your var "$imgurl[0]" is empty :

try something like this :

$images = new SimpleXMLElement($data);
$xmlImgUrl = array();
foreach($images as $image){
    foreach($image->image as $indimage){
        $xmlImgUrl[] = $indimage->url;
        $insiquery = "INSERT INTO images (id, artist, url) VALUES (NULL, '$artist','".$indimage->url."')"; 
        mysql_query($insiquery);
    }
}
}
$imgurl = $nrows == 0 ? $xmlImgUrl : mysql_fetch_row($iquery);
if(!empty($imgurl)) echo "<img src=\"".$imgurl[0]."\" alt=\"$artist image\">";

which instantiate an array of image urls when no results in mysql.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜