我正在创建一个网站,我希望用户能够开始输入乐队名称(例如,“Rad”)并让 Discogs API 向他们显示 10 个最相似的建议(例如,“Radical Face” 、“无线电头”等)。这些建议可以按字母顺序排序,理想情况下,可以按受欢迎程度排序。
问题是我不知道如何向 Discogs API 发出这样的请求。这是我现在正在使用的代码,它检索http://api.discogs.com/releases/1的内容并对其进行解析。
任何见解将不胜感激。谢谢你。
<?php
$url = "http://api.discogs.com/releases/1"; // add the resource info to the url. Ex. releases/1
//initialize the session
$ch = curl_init();
//Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_USERAGENT, 'SiteName/0.1 +http://your-site-here.com');
//Set the URL of the page or file to download.
curl_setopt($ch, CURLOPT_URL, $url);
//Ask cURL to return the contents in a variable instead of simply echoing them
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Execute the curl session
$output = curl_exec($ch);
//close the session
curl_close ($ch);
function textParser($text, $css_block_name){
$end_pattern = '], "';
switch($css_block_name){
# Add your pattern here to grab any specific block of text
case 'description';
$end_pattern = '", "';
break;
}
# Name of the block to find
$needle = "\"{$css_block_name}\":";
# Find start position to grab text
$start_position = stripos($text, $needle) + strlen($needle);
$text_portion = substr($text, $start_position, stripos($text, $end_pattern, $start_position) - $start_position + 1);
$text_portion = str_ireplace("[", "", $text_portion);
$text_portion = str_ireplace("]", "", $text_portion);
return $text_portion;
}
$blockStyle = textParser($output, 'styles');
echo $blockStyle. '<br/>';
$blockDescription = textParser($output, 'description');
echo $blockDescription. '<br/>';
?>