Heim > Backend-Entwicklung > PHP-Tutorial > php双重循环问题

php双重循环问题

WBOY
Freigeben: 2016-06-06 20:19:15
Original
3114 Leute haben es durchsucht

我想用php输出我在网易云音乐的歌单内容,先使用一个foreach输出歌单名称和id,再使用上一次输出的id来输出歌单内容。但是输出歌单内容就不知道该怎样写了...求大神帮助

<code><?php header('Content-type: application/json');
            header("Content-type: text/html; charset=utf-8");
            $refer = "http://music.163.com/";
            $header[] = "Cookie: " . "appver=1.5.0.75771;";
            $url = "http://music.163.com/api/user/playlist/?offset=0&limit=1001&uid=52792247";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
            curl_setopt($ch, CURLOPT_REFERER, $refer);
            $cexecute = curl_exec($ch);
            curl_close($ch);
            $obj = json_decode($cexecute);
            $result = $obj->playlist;
            foreach($result as $row){
            echo $row->name;
            echo $row->id;
            echo $row->coverImgUrl;
            }
?></code>
Nach dem Login kopieren
Nach dem Login kopieren

上面的代码可以正常输出,在有多个id的情况下该怎样循环输出http://music.163.com/api/playlist/detail?id=刚刚输出的id的内容?

回复内容:

我想用php输出我在网易云音乐的歌单内容,先使用一个foreach输出歌单名称和id,再使用上一次输出的id来输出歌单内容。但是输出歌单内容就不知道该怎样写了...求大神帮助

<code><?php header('Content-type: application/json');
            header("Content-type: text/html; charset=utf-8");
            $refer = "http://music.163.com/";
            $header[] = "Cookie: " . "appver=1.5.0.75771;";
            $url = "http://music.163.com/api/user/playlist/?offset=0&limit=1001&uid=52792247";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
            curl_setopt($ch, CURLOPT_REFERER, $refer);
            $cexecute = curl_exec($ch);
            curl_close($ch);
            $obj = json_decode($cexecute);
            $result = $obj->playlist;
            foreach($result as $row){
            echo $row->name;
            echo $row->id;
            echo $row->coverImgUrl;
            }
?></code>
Nach dem Login kopieren
Nach dem Login kopieren

上面的代码可以正常输出,在有多个id的情况下该怎样循环输出http://music.163.com/api/playlist/detail?id=刚刚输出的id的内容?

又是你!!!

获取用户歌单列表的API是POST请求,且参数做了加密,参考这里,这里就不深入了

php双重循环问题

<code><?php set_time_limit(0);
$getUserPlayListUrl = 'https://music.163.com/eapi/batch';
$playList = getUserPlayList($getUserPlayListUrl);
$obj = json_decode($playList);
if ($obj->code != 200) {
    exit('fetch user play list fail');
}
$list = $obj->{'/api/user/playlist/'}->playlist;
if (! count($list)) {
    exit('play list is empty');
}
echo "<ul>";
foreach ($list as $row) {
    echo "<li>";
    echo $row->name;
    if ($row->trackCount > 0) {
        $url = 'http://music.163.com/api/playlist/detail?id=' . $row->id;
        $json = file_get_contents($url);
        $obj = json_decode($json);
        
        if (! is_object($obj) || $obj->code != 200) {
            echo ('fetch data fail');
        } else {
            $result = $obj->result;
            if (count($result->tracks)) {
                echo "<ol>";
                foreach ($result->tracks as $key => $item) {
                    echo "<li><a href="%5C%22%24item-">mp3Url\">" . $item->name . "</a></li>";
                }
                echo "</ol>";
            }
        }
    }
    echo "</li>";
}
echo "</ul>";

function getUserPlayList($getUserPlayListUrl)
{
    $post = 'params=0BD8BB39A78692F1744DEFF63EBC30F729D0E608EFA82F71B26D52E0DD14D451D78F0ABC6D7BDA02BF0D4662472C4667732EC2CE6CCB09F52B990259A31B218B236C646BD84734B5ED93C3D4B15211C2344B0A37B52DE7DBCA4228F268BF216F4472C8152722BE401E349A19A19496EB20827E0B64188D5B17700794D44F2054FF2FF9A73D752E3EEA2F2B37ECAD586C02B9E94CFC99A25D784EFE5E4F73615A280B30FFADBDEAEF883462016EF031096504D5555D76D3D469C32E6D9B48B12B7B45B8FDEFB56D11E0DACA5E8EA3E4F3C10FDC95CECBCC2B5B4FD2EB52528AF1111F98FFB3DB1D83E0EAA4F8F5AD590AB6DFAE4B91A4E8543B231AD3CA3581C8646F1FE873A6A402282DAEE8201F114F69E8F977710BA09970D90EFF9DD3D4A4F40074D15FF3134ECDDFBCC7DD88AF99B47B4F16768DBC2BE5CC8B617E8C288A03858C3E93FF2521AAB784773A76886219417F0733EF7A2CCD5534A31EAC59080EA7AAF479D7911FEEB4FD260C5218744A47234269CE5E90589D3182FD69EBB0E88AABF91E4FFE2EFA11FCE3DA53E5B050EF24EF35808173F54792C80D618A1A7D4F5FF46D894178899C61995A90A699FBAF54B3966BD3D72BF9CAE824705645A0FFD7A4D144935EB87CC449F2A4A30FC0CD5979458767011A8A4D5A0609986452C1216ED51F3E0934157D13D7EB6E36C04A34D81C5BC884D79B09187E68463CEE3C812472FD46174783A6AF8969A5BE816C07E5742036B7E13EB9F28C7808308446D2007568414E1DE66356BEA6309F508C2D0435495D6AEDACC3FBF406A4FDBF46E21DEA322479640E569E9493DB8712AD5AA1642DC55B873B345BAD80B48A42F7224F9EF69F6DC220045FEB67E76283FAFE7D8360414EA4CD0062A9BA0C248D763D07DB2EE4B90DE838DF13B65F350FBF9B8D709D70E81F6053DFB4CE42BEDE4DE69F4B280AF5756BCE1803F9F74591C20D39EE5E2E6EF4C4077CF3C82CAE525F90EFFDC199B9DD64D044FC2D518B4449A3FB06FF4DA2FA4AB50A4B8F51BE30DB7A165AC7074051E17D56012684239969F161ABCC48A923E26D4B0509BDD73FDBAE61E1B67701622C8541747FF32E74163974E0BAE66C93C605EFE17390C141AF46C68E2656D32BF3BF4EAB6C8449C9E60F31A6977AB9C56A775B84CDBFF5C4CC5E866F62FE256CA1E07B9B89C1222B18B2C3F516998A8CECA35D61421DA36D554B9EEBC9B4B96FC443B34DFBDF6F14693B849591EE2906DE1908F15E342C7994126297B1A1D26BDF6C6EB48E7CAD81CBC3CABDDB3C2FD2740B38AB9FBA5AF80F969A185BC6937CC61C669C677C6B314A04320E76CA1D39EB65ECD82C58CC';
    
    // 初始化
    $curl = curl_init($getUserPlayListUrl);
    $header = array();
    $header[] = 'User-Agent:  网易云音乐 3.3.0 rv:256 (iPhone; iPhone OS 9.2.1; zh_CN)';
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    // 不输出header头信息
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 保存到字符串而不是输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_POST, true);
    // 请求数据
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    // 是否抓取跳转后的页面
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}</code>
Nach dem Login kopieren
Verwandte Etiketten:
php
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage