大家好。
这是网易云音乐的搜索API。
当调用下面一段function,$word是英文时搜索结果正常,但中文的时候返回结果不正确。
为什么呢,请指教?
function music_search($word, $type=1)
{
$url = "http://music.163.com/api/search/pc";
$post_data = array(
's' => $word,
'offset' => '0',
'limit' => '20',
'type' => $type,
);
$referrer = "http://music.163.com/";
$URL_Info = parse_url($url);
$values = [];
$result = '';
$request = '';
foreach ($post_data as $key => $value) {
$values[] = "$key=" . urlencode($value);
}
$data_string = implode("&", $values);
if (!isset($URL_Info["port"])) {
$URL_Info["port"] = 80;
}
$request .= "POST " . $URL_Info["path"] . " HTTP/1.1\n";
$request .= "Host: " . $URL_Info["host"] . "\n";
$request .= "Referer: $referrer\n";
$request .= "Content-type: application/x-www-form-urlencoded\n";
$request .= "Content-length: " . strlen($data_string) . "\n";
$request .= "Connection: close\n";
$request .= "Cookie: " . "appver=1.5.0.75771;\n";
$request .= "\n";
$request .= $data_string . "\n";
$fp = fsockopen($URL_Info["host"], $URL_Info["port"]);
fputs($fp, $request);
$i = 1;
while (!feof($fp)) {
if ($i >= 15) {
$result .= fgets($fp);
} else {
fgets($fp);
$i++;
}
}
fclose($fp);
return $result;
}
是不是你用的编码不一样