php双重循环问题
我想用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>
上面的代码可以正常输出,在有多个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>
上面的代码可以正常输出,在有多个id的情况下该怎样循环输出http://music.163.com/api/playlist/detail?id=刚刚输出的id
的内容?
又是你!!!
获取用户歌单列表的API是POST请求,且参数做了加密,参考这里,这里就不深入了
<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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
