• negodito over 5 years ago

    Hello, I was using the discogs api perfectly in my website discolado.com.br until the last week, but suddenly I received an error (bellow). We already tried everything to solve, but hadn't success. The Discogs support ask if we have a User-Agent defined. I don't know because I'm not a developer, could somebody help please? Bellow the errors I'd pasted the codes. Maybe we were blocked by discogs? Thank you!

    Error---------------------------------------------

    Warning: file_get_contents(http://www.discogs.com/search?type=releases&f=xml&api_key=186c0f2c8c&q=nirvana) [function.file-get-contents]: failed to open stream: HTTP request failed! 500 Client Refused in /home/storage/f/ad/92/discolado/public_html/joomla/administrator/components/com_muscol/controllers/albums.php on line 61

    Warning: simplexml_load_file() [function.simplexml-load-file]: discogs105.xml:1: parser error : Document is empty in /home/storage/f/ad/92/discolado/public_html/joomla/administrator/components/com_muscol/controllers/albums.php on line 73

    Warning: simplexml_load_file() [function.simplexml-load-file]: in /home/storage/f/ad/92/discolado/public_html/joomla/administrator/components/com_muscol/controllers/albums.php on line 73

    Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/storage/f/ad/92/discolado/public_html/joomla/administrator/components/com_muscol/controllers/albums.php on line 73

    Warning: simplexml_load_file() [function.simplexml-load-file]: discogs105.xml:1: parser error : Start tag expected, "<" not found in /home/storage/f/ad/92/discolado/public_html/joomla/administrator/components/com_muscol/controllers/albums.php on line 73

    Warning: simplexml_load_file() [function.simplexml-load-file]: in /home/storage/f/ad/92/discolado/public_html/joomla/administrator/components/com_muscol/controllers/albums.php on line 73

    Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/storage/f/ad/92/discolado/public_html/joomla/administrator/components/com_muscol/controllers/albums.php on line 73

    Fatal error: Call to a member function children() on a non-object in /home/storage/f/ad/92/discolado/public_html/joomla/administrator/components/com_muscol/controllers/albums.php on line 91

    The search function---------------------------------
    (I think the problem is here, at the line 'header' => 'Accept-Encoding: gzip\r\n', but I don't know how to solve...

    function search_discogs(){
    global $mainframe;

    error_reporting(0) ;

    $query = JRequest::getVar('q');
    $query = urlencode($query);

    $params = &JComponentHelper::getParams( 'com_muscol' );

    $options = array('http' =>
    array(
    'method' => 'GET',
    'header' => 'Accept-Encoding: gzip\r\n'
    )
    );
    $context = stream_context_create($options);
    $return = file_get_contents('http://www.discogs.com/search?type=releases&f=xml&api_key='.$params->get('discogs_api_key').'&q=' . $query, false, $context);

    //$return = file_get_contents('http://www.discogs.com/search?type=releases&f=xml&api_key=186c0f2c8c&q=' . $query);

    $usuari =& JFactory::getUser();
    $write_filename = "discogs".$usuari->id.".xml" ;

    JFile::write($write_filename, $return);

    $gzip = new JArchiveGzip();

    $gzip->extract($write_filename, $write_filename);
    $xml = simplexml_load_file($write_filename);

    $search = JRequest::getVar('search');
    switch($search){
    case "tracklist":
    $js_function = "get_discogs_release_tracklist" ;
    $button_title = JText::_('Get tracklist') ;
    break;
    default:
    $js_function = "get_release" ;
    $button_title = JText::_('Get release data') ;
    break;
    }

    /*$uncompressed_string = gzuncompress($return) ;
    $xml = simplexml_load_string( $uncompressed_string );*/

    foreach($xml->children() as $child){
    foreach($child->children() as $child2){

    echo "<div class="discogs_release">" ;
    foreach($child2->children() as $child3){

    if($child3->getName() == "title") echo "" . $child3. "<br/>";
    elseif($child3->getName() == "summary") echo $child3. "<br/>";
    elseif($child3->getName() == "uri"){
    $pieces = explode("/", $child3) ;
    $release_id = $pieces[count($pieces) -1];
    echo "" . $button_title . "<br/>";
    }

    }
    echo "</div>";
    if($search != "tracklist") echo "<div id="discogs_release_".$release_id."" class="individual_release"></div>";
    }
    }

    JFile::delete($write_filename);

    //echo $return;
    $mainframe->close();

    }
  • vreon over 5 years ago

    Yeah, it looks like your client was blocked because it doesn't supply a User-Agent. User-Agents help us identify clients that are behaving badly, and also give us a way to get in contact with the developer.

    Normally you'd do this with PHP's cURL bindings, but since you're using file_get_contents() I think you can do something like this instead.

    ini_set('user_agent', 'DiscoladoBot/1.0 +http://discolado.com.br')

    Either make sure that line is run at least once before any call to file_get_contents() runs, or add the appropriate directive to your php.ini.

Log In You must be logged in to post.