将 HTTPS 替换为 XML 的 HTTPS
Replace HTTPS to HTTPS of XML
我需要从 XML 文件中获取图像 url,但我的服务器出现一些错误,因为此 url 使用 https。如何读取 https link 并将其重新定位为 http?请帮助我,我不懂编程...
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 ];
}
谢谢!
正如我在评论中提到的,为此使用 str_replace()
。
我的示例假设 get()
方法 returns 一个带有 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 的情况下可用。
我需要从 XML 文件中获取图像 url,但我的服务器出现一些错误,因为此 url 使用 https。如何读取 https link 并将其重新定位为 http?请帮助我,我不懂编程...
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 ];
}
谢谢!
正如我在评论中提到的,为此使用 str_replace()
。
我的示例假设 get()
方法 returns 一个带有 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 的情况下可用。