I
I
Igor Maksimov2015-10-03 13:50:48
PHP
Igor Maksimov, 2015-10-03 13:50:48

How to output data from if to a separate variable?

There is a cover parser from last.fm , though a bit unfinished. The task is this: the parser goes to last.fm and rips out the artist's cover, if the cover is missing, the default poster is displayed. All this works out for me echo , but instead of echo I need to organize the data output into a variable and transfer it to the database. Actually the parser code.

<?php

$name = "cosmic gate";

$array = array(
           '/\./'               =>'',
           '/( feat)(.*)/'   =>'',
           '/( Ft)(.*)/'     =>'',
           '/( ft)(.*)/'     =>'',
           '/( Feat)(.*)/'   =>'',
           '/( pres)(.*)/'   =>'',
           '/( Pres)(.*)/'   =>'',
           '/( vs)(.*)/'     =>'',
           '/( Vs)(.*)/'     =>''

        );

$artist = preg_replace(array_keys($array),array_values($array),$name);
$url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&api_key=57ee3318536b23ee81d6b27e36997cde&artist='.$artist;
$xml = @simplexml_load_file($url);
$poster_def = "<img src=\"images/logo.png\"/>";

if ($xml === false) {
  echo $poster_def;

} else {

  if(!empty($xml->artist->image[3])) {

       $poster_last  = $xml->artist[0]->image[3];
       $poster_name  = uniqid() . ".png";
       $poster_dir   = "images";
       $poster_pach  = $poster_dir . "/" . $poster_name;
       echo $xml->artist[0]->image[3];

       file_put_contents($poster_pach, file_get_contents($poster_last));

  } else {

    echo $poster_def;
}
}


?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
veydlin, 2015-10-03
@crunchuser

Function

function parse($name){
    $array = array(
               '/\./'            =>'',
               '/( feat)(.*)/'   =>'',
               '/( Ft)(.*)/'     =>'',
               '/( ft)(.*)/'     =>'',
               '/( Feat)(.*)/'   =>'',
               '/( pres)(.*)/'   =>'',
               '/( Pres)(.*)/'   =>'',
               '/( vs)(.*)/'     =>'',
               '/( Vs)(.*)/'     =>''
    );
    $artist = preg_replace(array_keys($array),array_values($array),$name);
    $url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&api_key=57ee3318536b23ee81d6b27e36997cde&artist='.$artist;
    $xml = @simplexml_load_file($url);
    $poster_def = "<img src=\"images/logo.png\"/>";

    if($xml === false || empty($xml->artist->image[3])) {
        return $poster_def;
    } 

    $poster_last  = $xml->artist[0]->image[3];
    $poster_name  = uniqid() . ".png";
    $poster_dir   = "images";
    $poster_pach  = $poster_dir . "/" . $poster_name;
    file_put_contents($poster_pach, file_get_contents($poster_last));

    return $xml->artist[0]->image[3];
}

$myVar = parse('cosmic gate');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question