Warning: simplexml_load_string()

Asked

Viewed 763 times

1

Hello,

I’m with that 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

Along those lines:

$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) {
  • and what is your question ?

  • edited and added $mp3_search

  • @feliphefelix It worked?

  • Yes, Gave the internet dropped here and could not warn, Ta raining a lot.

1 answer

2


I think you should be looking to use the function file_get_contents and not get_content.

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;
}

But as the returned data are in format json, the function is not required simplexml_load_string, but yes json_decode:

$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 function parameter json_decode is defined as true so that it is possible to use the returned value as a associative array.

DEMO

Browser other questions tagged

You are not signed in. Login or sign up in order to post.