Search code examples
phpxmlreplacelast.fm

Replace HTTPS to HTTPS of XML


I need to get an image url from a XML file, but my server got some errors because this url uses https. How can I read the https link and relace it to http? Please help me, I don't understand programming...

function api_lastfm( $artist, $api_key ) {
        $data = xml2array( get( "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" . urlencode( $artist ) . "&api_key={$api_key}", false, false, false, 6 ) );
        return ( isset( $data[ 'artist' ][ 'image' ][ 4 ] ) && !empty( $data[ 'artist' ][ 'image' ][ 4 ] ) ) ? $data[ 'artist' ][ 'image' ][ 4 ] : $data[ 'artist' ][ 'image' ][ 3 ];
    }

enter image description here

Thanks!


Solution

  • Like I mentioned in my comment, use str_replace() for this.

    My example assumes that the get() method returns a string with the xml:

    function api_lastfm( $artist, $api_key ) 
    {
        $data = get( "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" . urlencode( $artist ) . "&api_key={$api_key}", false, false, false, 6 );
    
        // Replace all image urls using https to http
        $data = str_replace('https://lastfm-img2', 'http://lastfm-img2', $data);
    
        $data = xml2array($data);
        return ( isset( $data[ 'artist' ][ 'image' ][ 4 ] ) && !empty( $data[ 'artist' ][ 'image' ][ 4 ] ) ) ? $data[ 'artist' ][ 'image' ][ 4 ] : $data[ 'artist' ][ 'image' ][ 3 ];
    }
    

    This should change all image URL's from https to http. It does mean that the images must be available without https as well, though.