topical media & game development

talk show tell print

mashup-delicious-06-example6-8-index.php / php



  <?php
  //bring in Snoopy goodness
  include('Snoopy.class.php');
  
  //create a new Snoopy object
  snoopy = new Snoopy;
  
  //set UserAgent and WWW-Authenticate information
  snoopy->agent = 'example6-8 1.0';
  snoopy->user = 'username';
  snoopy->pass = 'password';
  
  //give snoopy our path to cURL and where it should put downloaded files
  snoopy->curl_path = '/usr/bin/curl'; //Windows: C:\curl
  snoopy->temp_dir = '/tmp'; //Windows: C:\temp
  
  //the REST url we are to use
  url = "https://api.del.icio.us/v1/posts/get";
  
  //the data array that will store our parsed xml
  xml_data_array = array();
  
  //fetch the REST URL
  if(snoopy->fetch(url))
  {
      //snoopy doesn't set status with ssl requests, so let’s do that here
      if (snoopy->status == '')
          snoopy->status = get_status_from_response(snoopy->response_code);
      //if we've got a 200 response code, show the results, otherwise warn
      if (snoopy->status == 200) {
          parse_xml_results(snoopy->results);
          echo "<pre>";
          print_r(xml_data_array);
          echo "</pre>";
      } else {
          echo "Warning: ".snoopy->response_code;
      }
  } 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];
  }
  
  //this is an adaptation of the xml parsing code from example6-76.php
  //turned into a function. it takes snoopy's results and parses it as xml
  function parse_xml_results(snoopy_results)
  {
      global xml_data_array;
  
      //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 element
  function process_start(parser, name, attributes)
  {
      global xml_data_array;
      element_data = array("name"=>name,"attributes"=>attributes); 
      array_push(xml_data_array,element_data);
  }
  
  //handle the end of an element
  function process_end(parser, name)
  {
      global xml_data_array;
      xml_data_array[count(xml_data_array)-2]['children'][] =
          xml_data_array[count(xml_data_array)-1];
      array_pop(xml_data_array);
  }
  
  //handle the character data (content) of an element
  function process_cdata(parser, cdata)
  {
     global xml_data_array,i;
  
     if(trim(cdata))
     {   
         xml_data_array[count(xml_data_array)-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.