Warning: simplexml_load_string ()

1

Hello,

I have this warning in my code:

  

Warning: simplexml_load_string (): Entity: line 1: parser error: Start   tag expected, '<' not found in /var/www/modules/search.php on line 97   Warning: simplexml_load_string (): {"errors": [{"error_message": "401 -   Unauthorized "}]} in /var/www/modules/search.php on line 97 Warning:   simplexml_load_string (): ^ in /var/www/modules/search.php on line 97   Warning: Invalid argument supplied for foreach () in   /var/www/modules/search.php on line 99

On this line:

$mp3_search = get_content("http://api.soundcloud.com/tracks/?client_id=2e2135723532d1c7a5134501cb2e923c&q=" . $keyword);

if ($mp3_search)
{
    $mp3_search = simplexml_load_string($mp3_search);

    foreach ($mp3_search as $result) {
    
asked by anonymous 15.02.2015 / 21:49

1 answer

2

I think you should be looking to use file_get_contents instead of% with%.

Your code should look like this:

$mp3_search = file_get_contents("http://api.soundcloud.com/tracks/?client_id=2e2135723532d1c7a5134501cb2e923c&q=" . $keyword);

$resultado = simplexml_load_string($mp3_search);
foreach ($resultado as $item) {
    echo $item. PHP_EOL;
}

However, since the returned data is in json format, the get_content ", but simplexml_load_string :

$keyword = "Trance";
$mp3_content = file_get_contents("http://api.soundcloud.com/tracks/?client_id=2e2135723532d1c7a5134501cb2e923c&q=" . $keyword);

$jsonObj = json_decode($mp3_content, true);

foreach($jsonObj as $item){
    echo $item['title']. PHP_EOL;
}

The second parameter of the json_decode function is set to true so that you can use the returned value as a associative array .

DEMO

    
15.02.2015 / 22:03