Текст JSON не разбирается при использовании cURL, чтобы захватить его?

У меня есть действительно длинная строка JSON: http://pastebin.com/2jJKSGHs , который извлекается из музыкального API.

У меня есть этот код для его анализа ( http://pastebin.com/EuJtuhHg ):

$url = "https://api.discogs.com/database/search?type=artist&q=pink[keyandsecretredacted]";

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

//Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_USERAGENT, 'YourSite/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);//decode and print output
$output = json_decode($output2, true);
echo($output['results'][0]['title']);

Когда я вставляю содержимое строки JSON непосредственно в мой код, json_decode отлично с ним работает. Но когда я пытаюсь извлечь его из API с помощью описанного выше метода, на моей странице ничего не печатается — оно просто пустое. Распечатка json_last_error возвращает «0», поэтому она не обнаруживает никаких ошибок.

Есть идеи, почему это может происходить?

0

Решение

замещать

$output = curl_exec($ch);

с

$output2 = curl_exec($ch);

Иначе $output2 не определено, и json_decode использует неопределенную переменную:

$output = json_decode($output2, true);
1

Другие решения

Других решений пока нет …