Get из Discogs API с PHP и выходными значениями

Я использую следующий код для получения данных из определенной записи из Discogs:

//initialize the session
$ch = curl_init();

//Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_USERAGENT, 'MYAPPNAME/0.1 +http://ymysite.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);

Я могу вывести вывод $, который даст мне очень сложный вывод:

{"styles": ["Blues Rock", "Rock & Roll"], "videos": [{"duration": 200, "embed": true, "title": "The Rolling Stones - Street Fighting Man", "description": "The Rolling Stones - Street Fighting Man", "uri": " http://www.youtube.com/watch?v=qUO8ScYVeDo"}, {"duration": 2457, "embed": true, "title": "Банкет Роллинг Стоунз - Нищие FULL ALBUM (моно винил микс)", "description": "Банкет Роллинг Стоунс - Нищие FULL ALBUM (моно винил микс)", "uri": " http://www.youtube.com/watch?v=sRu88xttBrA"}]," series ": []," tags ": [{" resource_url ":" http://api.discogs.com/labels/5320"," entity_type ": "1", "catno": "SKL 4955", "id": 5320, "name": "Decca"}]... Есть еще...

Как, например, я могу получить информацию из "стилей" с помощью PHP, чтобы она выводила "Blues Rock, Rock & Roll"? Может быть, я также хочу получить информацию из "описания" для вывода "The Rolling Stones - Beggars Banquet FULL ALBUM (моно винил микс)".

С наилучшими пожеланиями, Йохан

1 ответ

Решение
<?php
$output = '{"styles": ["Blues Rock", "Rock & Roll"], "videos": [{"duration": 200, "embed": true, "title": "The Rolling Stones - Street Fighting Man", "description": "The Rolling Stones - Street Fighting Man", "uri": "http://www.youtube.com/watch?v=qUO8ScYVeDo"}, {"duration": 2457, "embed": true, "title": "The Rolling Stones - Beggars Banquet FULL ALBUM (mono vinyl mix)", "description": "The Rolling Stones - Beggars Banquet FULL ALBUM (mono vinyl mix)", "uri": "http://www.youtube.com/watch?v=sRu88xttBrA"}], "series": [], "labels": [{"resource_url": "http://api.discogs.com/labels/5320", "entity_type": "1", "catno": "SKL 4955", "id": 5320, "name": "Decca"}]...There is more...';

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/>';

Это поможет вам захватить все регионы, которые вам нужны, вам нужно будет изменить скрипт по мере необходимости. Надеюсь, это поможет вам:)

Другие вопросы по тегам