Encoding XML Url中带有特殊字符的simpleXML

Encoding XML Url中带有特殊字符的simpleXML,encoding,utf-8,simplexml,Encoding,Utf 8,Simplexml,我的代码有点问题。我正试图从Spotify获取歌曲id。 这对我来说很好,但对于url中的特殊字符(如ü,ö,ä)就不行了 我收到此警告(第一行是回响的Url): 这是我的代码: function getID($artist,$title){ $url = utf8_decode("http://ws.spotify.com/search/1/track?q=".$artist."+AND+track:".$title); echo $url; $xml = simplexml_loa

我的代码有点问题。我正试图从Spotify获取歌曲id。 这对我来说很好,但对于url中的特殊字符(如ü,ö,ä)就不行了

我收到此警告(第一行是回响的Url):

这是我的代码:

function getID($artist,$title){

$url = utf8_decode("http://ws.spotify.com/search/1/track?q=".$artist."+AND+track:".$title);
echo $url;
    $xml = simplexml_load_file($url);
    $id= $xml->track->attributes();
    return($id);

 }

echo getID("Sportfreunde+Stiller","Frühling");
如何解决这个问题?我尝试了utf_encode(),但没有成功


谢谢大家!

这与UTF-8编码无关,当然也与名称不正确的
utf8\u解码
/
utf8\u编码
函数无关,这些函数实际上应该命名为
utf8\u to_iso8859\u 1
/
iso8859\u 1\u to_utf8
,很少是任何编码问题的正确答案

严格来说,URL只能包含一组非常有限的字符——基本字母、数字和一些标点符号。其他任何内容都必须使用类似于
%3f
的序列进行转义

要正确转义URL中的字符串,可以使用PHP函数,例如

function getID($artist,$title){

$url = utf8_decode("http://ws.spotify.com/search/1/track?q=".$artist."+AND+track:".$title);
echo $url;
    $xml = simplexml_load_file($url);
    $id= $xml->track->attributes();
    return($id);

 }

echo getID("Sportfreunde+Stiller","Frühling");
$url = "http://ws.spotify.com/search/1/track?q=" . urlencode($artist) . "+AND+track:" . urlencode($title);