从PHP数据中获取信息

从PHP数据中获取信息

问题描述:

I have no idea even what this is called so had no idea what to search for. I do apologise if this already exists.

I have information that I can get from going to a specific URL. It displays with something that looks like this:

 {"loading":false,"playing":true,"position":5730,"duration":211875,"index":0,"repeat":false,"shuffle":false,"volume":1,"context":null,"contexts":[],"track":{"artists":[{"name":"R3hab","uri":"spotify:artist:6cEuCEZu7PAE9ZSzLLc2oQ"},{"name":"Vinai","uri":"spotify:artist:4mrBetqy378Jf1y6NLszlx"}],"disc":0,"duration":212000,"image":"spotify:image:9ab80608da23ad97a5fd1cd8285f7a9239169c9d","images":[[64,"spotify:image:544aab2c02ea3ece23145ac3fdb78649e1181cad"],[300,"spotify:image:9ab80608da23ad97a5fd1cd8285f7a9239169c9d"],[600,"spotify:image:8537cde9090567164f449b25c00ad0c40e5c7325"]],"name":"How We Party - Original Mix","number":1,"playable":true,"popularity":67,"starred":false,"explicit":false,"availability":"premium","album":{"uri":"spotify:album:2IhQHv3MS8iq26We33dLdQ"},"local":false,"advertisement":false,"placeholder":false,"uri":"spotify:track:2b0N6oW4f87wNr9uahicc2"}}

If you scroll half through you'll see:

"name":"How We Party - Original Mix

I need a PHP script that would find the name of the track and displays it like this:

How We Party - Original Mix

Beware that the code has the word "name" in it several times i.e. name of album, which I don't need. The position of order is always the same so that may help.

I have no idea how to even start going about this, so any help would be greatly appreciated.

我甚至不知道这是什么,所以不知道要搜索什么。 如果这已经存在,我很抱歉。 p>

我可以从转到特定网址获得相关信息。 它显示的内容如下所示: p>

  {“loading”:false,“playing”:true,“position”:5730,“duration”:211875,“index  “:0,” 重复 “:假,” 洗牌 “:假,” 体积 “:1,” 上下文 “:NULL,” 上下文 “:[],” 轨道 “:{” 艺术家 “:[{” 名称“:  “R3hab”, “URI”: “的Spotify:艺术家:6cEuCEZu7PAE9ZSzLLc2oQ”},{ “名称”: “威奈”, “URI”: “的Spotify:艺术家:4mrBetqy378Jf1y6NLszlx”}], “盘”:0, “持续时间”:  212000, “图像”: “的Spotify:图像:9ab80608da23ad97a5fd1cd8285f7a9239169c9d”, “图像”:[[64, “Spotify的:图像:544aab2c02ea3ece23145ac3fdb78649e1181cad”],[300, “Spotify的:图像:9ab80608da23ad97a5fd1cd8285f7a9239169c9d”],[600,“Spotify的:图像 :8537cde9090567164f449b25c00ad0c40e5c7325“]],”名称“:”我们如何聚会 - 原创混音“,”数字“:1,”可玩“:真实,”人气“:67,”已加星标“:虚假,”明确“:虚假,” 可用性 “:” 溢价 “ ”专辑“:{ ”URI“: ”Spotify的:专辑:2IhQHv3MS8iq26We33dLdQ“}, ”本地“:假的, ”广告“:假的, ”占位符“:假的, ”URI“:” Spotify的:  track:2b0N6oW4f87wNr9uahicc2“}} 
  code>  pre> 
 
 

如果你向前滚动一半 我会看到: p>

 “name”:“How We Party  -  Original Mix 
  code>  pre> 
 
 

我需要一个PHP 找到轨道名称的脚本,并显示如下: p>

  How We Party  -  Original Mix 
  code>  pre> 
 
 请注意代码中有几个单词“name”,即专辑名称,我不需要。 订单的位置总是相同的,这可能有所帮助。 p> 
 
 

我不知道如何开始这样做,所以任何帮助将不胜感激。 p> div>

That string is JSON. To parse it you would use json_decode(). Then you can access it like an object (or an array if you pass true as the second parameter).

$str = '{"loading":false,"playing":true,"position":5730,"duration":211875,"index":0,"repeat":false,"shuffle":false,"volume":1,"context":null,"contexts":[],"track":{"artists":[{"name":"R3hab","uri":"spotify:artist:6cEuCEZu7PAE9ZSzLLc2oQ"},{"name":"Vinai","uri":"spotify:artist:4mrBetqy378Jf1y6NLszlx"}],"disc":0,"duration":212000,"image":"spotify:image:9ab80608da23ad97a5fd1cd8285f7a9239169c9d","images":[[64,"spotify:image:544aab2c02ea3ece23145ac3fdb78649e1181cad"],[300,"spotify:image:9ab80608da23ad97a5fd1cd8285f7a9239169c9d"],[600,"spotify:image:8537cde9090567164f449b25c00ad0c40e5c7325"]],"name":"How We Party - Original Mix","number":1,"playable":true,"popularity":67,"starred":false,"explicit":false,"availability":"premium","album":{"uri":"spotify:album:2IhQHv3MS8iq26We33dLdQ"},"local":false,"advertisement":false,"placeholder":false,"uri":"spotify:track:2b0N6oW4f87wNr9uahicc2"}}';

// object
$obj = json_decode($str);
echo $obj->track->name;

// array
$obj = json_decode($str, true);
echo $obj['track']['name'];

Demo