topical media & game development

talk show tell print

mashup-delicious-15-rally2007-lib-delicious-mashups.inc.php / php



  <?php
  //you must instantiate the Snoopy class first
  if (!class_exists('Snoopy'))
      die('Please include the Snoopy class library before delicious_mashups.inc.php is included.');
  
  //you also must define DELICIOUS_USERNAME and DELICIOUS_PASSWORD
  if (!defined('DELICIOUS_USERNAME') || !defined('DELICIOUS_PASSWORD'))
      die('Please make sure DELICIOUS_USERNAME and DELICIOUS_PASSWORD are set before delicious_mashups.inc.php is included.');
  //the base del.icio.us REST API URL
  
  define('DELICIOUS_API_URL',"https://api.del.icio.us/v1");
  
  //create a new Snoopy object
  snoopy = new Snoopy;
  
  //set UserAgent and WWW-Authenticate information
  snoopy->agent = "del.icio.us-Mashups-Library/1.0";
  snoopy->user = DELICIOUS_USERNAME;
  snoopy->pass = DELICIOUS_PASSWORD;
  
  //give snoopy our path to cURL and where it should put downloaded files
  snoopy->curl_path = '/usr/bin/curl'; //Windows: C:\curl\curl.exe
  snoopy->temp_dir = '/tmp'; //Windows: C:\temp
  
  //hide notices as Snoopy generates some
  error_reporting(E_ERROR | E_WARNING | E_PARSE);
  
  api_return = array();
  
  function do_api_call(method, query_string = '')
  {
      //bring in and setup api_return
      global api_return;
      api_return = array();
      
      //bring in snoopy
      global snoopy;
      
      //setup the full api url
      url = DELICIOUS_API_URL.method.query_string;
      
      //sleep for one second as per request
      sleep(1);
      
      //fetch the REST URL with snoopy
      if(snoopy->fetch(url))
      {
          //snoopy doesn't set status with ssl requests, so lets do that here
          if (snoopy->status == '')
              snoopy->status = get_status_from_response(snoopy->response_code);
          //if we've got a 200 response code then parse our XML
          if (snoopy->status == 200)
              parse_xml_results(snoopy->results);
          //give api_return the status code
          api_return['status'] = snoopy->status;
      } else {
          //handle any snoopy errors by outputting them to the screen
          die("ERROR: ".snoopy->error);
      }
  }
  
  //this function will pull the status code from snoopy's response_code var
  //usually snoopy sets this but not with SSL connections
  function get_status_from_response(response_code)
  {
      preg_match("|^HTTP/[^\s]*\s(.*?)\s|",response_code, status);
      return status[1];
  }
  
  //takes the api results and parses it as xml returning the data as an array
  function parse_xml_results(snoopy_results)
  {
      //bring in the api_return variable
      global api_return;
      
      //setup the parser and handlers
      parser = xml_parser_create();
      xml_set_element_handler(parser, "process_start", "process_end");
      xml_set_character_data_handler(parser, "process_cdata");
  
      //read from the results, parsing its contents
      parsed = xml_parse(parser, snoopy_results);
      if (!parsed)
         die(xml_error_string(xml_get_error_code(parser)." at line 
  ".xml_get_current_line_number(parser)));
  
      //clear the parser
      xml_parser_free(parser);
  }
  
  //handle the start of an XML element
  function process_start(parser, name, attributes)
  {
      global api_return;
      element_data = array("name"=>name,"attributes"=>attributes); 
      array_push(api_return,element_data);
  }
  
  //handle the end of an XML element
  function process_end(parser, name)
  {
      global api_return;
      api_return[count(api_return)-2]['children'][] = api_return[count(api_return)-1];
      array_pop(api_return);
  }
  
  //handle the character data (content) of an XML element
  function process_cdata(parser, cdata)
  {
     global api_return,i;
     if(trim(cdata))
     {   
         api_return[count(api_return)-1]['cdata']=cdata;   
     }
  }
  
  ?>


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.