如何将变量放入JSON URL? [关闭]

问题描述 投票:-2回答:1

尝试了很多组合,但没有任何作用。

$json = file_get_contents("http://www.theaudiodb.com/api/v1/json/58424d43204d6564696120/search.php?s=$artist");
php
1个回答
1
投票

如果$artist变量包含不适合在URL中使用的字符(如空格,标点符号等),我唯一能想到的就是“工作”。

PHP有一个函数可以使字符串安全地用于URL~urlencode() ...

$artist = 'Pink Floyd'; // spaces should be encoded as "%20" or "+"
$json = file_get_contents(sprintf(
    'http://www.theaudiodb.com/api/v1/json/58424d43204d6564696120/search.php?s=%s', 
    urlencode($artist)));
© www.soinside.com 2019 - 2024. All rights reserved.