Table of Contents
回复内容:
Home Backend Development PHP Tutorial php双重循环问题

php双重循环问题

Jun 06, 2016 pm 08:19 PM
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>
Copy after login
Copy after login

上面的代码可以正常输出,在有多个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>
Copy after login
Copy after login

上面的代码可以正常输出,在有多个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>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles