2016-08-20 29 views
-1

我需要从XML文件中获取图像URL,但由于此URL使用https,因此服务器出现了一些错误。我如何读取https链接并将其关联到http?请帮我,我不懂编程...将HTTPS替换为XML的HTTPS

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

谢谢!

+0

在转换XML响应(可能只是一个字符串)之前,请使用PHP的[str_replace](http://php.net/manual/en/function.str-replace.php)函数。像:'$ string = str_replace('https:// lastfm-img2','http:// lastfm-img2',$ string)'。但是你确定问题是因为它是一个https网址吗? –

+0

我的服务器日志显示此错误消息:“CURL请求”https://lastfm-img2.akamaized.net/i/u/549d31e77f614f269ca04a6bc156bda2.png“失败!日志:SSL证书问题:无法获取本地颁发者证书”。所以我想将https替换为http。请问,我的代码将如何?我把这个str_replace命令放在哪里?谢谢。 –

+0

我写了一个关于在哪里添加它的答案。这是一个猜测,因为我不知道你的get()方法返回了什么。 –

回答

0

就像我在我的评论中提到的,使用str_replace()这个。

我的例子假设get()方法返回一个字符串与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 ]; 
} 

这应该改变所有图像的URL从https为HTTP。这确实意味着图像必须在没有https的情况下才可用。

+0

它工作了!谢谢!! –

+0

@RodrigoCaetano - 如果这解决了您的问题,请将其标记为已回答。 –