-1

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!

Mestica
  • 1,489
  • 4
  • 23
  • 33
  • Before you convert the XML-response (which probably is just a string), use PHP's [str_replace](http://php.net/manual/en/function.str-replace.php) function. Like: `$string = str_replace('https://lastfm-img2', 'http://lastfm-img2', $string)`. But are you sure the problem is because it is a https url? – M. Eriksson Aug 20 '16 at 00:29
  • My server log display this error message: "CURL Request "https://lastfm-img2.akamaized.net/i/u/549d31e77f614f269ca04a6bc156bda2.png" failed! LOG: SSL certificate problem: unable to get local issuer certificate". So I'd like to replace the https to http. Please, how my code would be? Where I put this str_replace command? Thanks. – Rodrigo Caetano Aug 20 '16 at 00:49
  • I wrote an answer on where to do add it. It is a guess since I don't really know what your `get()` method returns. – M. Eriksson Aug 20 '16 at 00:58

1 Answers1

0

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.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40