Sign In
Extras For Your Programs

Below you will find some extra goodies that we decided you might want to have. If you have anything to contribute we will be more than happy to pack it up and send it along to the other people using the site, with a link back to your website. We can offer NO guarantee's that the code below will work. It's simply a baseline to work from and build on your own, please check out the Services tab and view the documentation specifically for that service.


Own an API?

We would really be happy if we incorporated your API into our system and you showed some link support by providing a link to our website. Here is a badge that is the same size as the Creative Commons badge -
the easy api badge


PHP5 Code

Here is an example of how to make a call with cURL to the system and get the zip code details for the zip code 90210.

cURL implementation:
// Initialize the CURL library
$cURL = curl_init();

// Set the URL to execute
curl_setopt($cURL, CURLOPT_URL, "http://xmlfeed.theeasyapi.com");

// Set options
curl_setopt($cURL, CURLOPT_HEADER, 0);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL, CURLOPT_POST, 1);
curl_setopt($cURL, CURLOPT_POSTFIELDS, "request=<easyapi_wrapper><login><apikey>{ your api key }</apikey></login><search><service>zipcode</service><criteria><zipcode>90210</zipcode></criteria></search></easyapi_wrapper>");

// Execute, saving results in a variable
$strPage = curl_exec($cURL);

// Close CURL resource
curl_close($cURL);

// Now the variable $strPage has the returned XML.
// Parse the XML into something a little more useful
$xml_ret = simplexml_load_string($strPage);
print_r($xml_ret);

non-cURL PHP5 Implementation:

function easyapi_send($url, $data){
   $params = array(
      'http' => array(
         'method' => 'POST',
         'header'=>
            'Accept-language: en\r\n'.
            'Content-type: application/x-www-form-urlencoded\r\n',
            'content'=>http_build_query(array('request'=>$data))
   ));
   
   $context = @stream_context_create($params);
   $fp = @fopen($url,'r',false,$context);
   if(!$fp){
      return '<wrapper_error><error>1</error><reason>The wrapper failed to get a valid connection with fopen.  Check to make sure that function works properly.</reason></wrapper_error>';
   }else{
      $response = @stream_get_contents($fp);
      if($response === false){
         return '<wrapper_error><error>1</error><reason>The wrapper attempted to get the response with the function stream_get_contents but could not.</reason></wrapper_error>';
      }else{
         return $response;
      }
   }
}

$returnedXML = easyapi_send('http://xmlfeed.theeasyapi.com','<easyapi_wrapper><login><apikey>{ your api key }</apikey></login><search><service>zipcode</service><criteria><zipcode>90210</zipcode></criteria></search></easyapi_wrapper>');

if(!empty($returnedXML)){
   print_r(simplexml_load_string($returnedXML));
}else{
   echo 'The returned data did not come through.';
}

 

Access with JAVA
A big thank you goes out to Peter Doan with Radian6 for allowing me to use their code to show in my extras section. This is to access the Google PageRank service and then get the PR back out of the system.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class GoogleUtils {
  ArrayList<String> URLList;

  URLConnection conn=null;

  static Logger logger = Logger.getLogger(GoogleUtils.class);

  ////////////////////////////////////////////////////////////
  //load URL from file and put into URLList array list
  void loadFile(String filename){
    try {
      BufferedReader reader=new BufferedReader(new FileReader(filename));
    
      String url;
    
      URLList=new ArrayList<String>();
    
      while ((url=reader.readLine()) !=null){
        URLList.add(url);
      }
    } catch (FileNotFoundException e){
      e.printStackTrace();
    }catch (IOException ioe){
      ioe.printStackTrace();
    }
  }

  /*open URL connection

   * */

  void getConnection(){
    try {
      URL url = new URL("http://xml.theeasyapi.com");
      conn=url.openConnection();
      conn.setDoOutput(true);
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  //call page rank

  void callPageRank(){
    try {
      String request, response;
      
      for(String url: URLList){
        System.out.print(url);
        getConnection();
        request=generateXMLRequestForPageRank(url);
        request= URLEncoder.encode("request", "UTF-8") + "=" + URLEncoder.encode(request, "utf-8");
        response=requestPageRank(request);
        processResponse(response);
      }
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  /* process response XML from Easy API

  * */

  void processResponse(String responseXML){
    try {
      if(!responseXML.equalsIgnoreCase("")){
        SAXBuilder parser = new SAXBuilder();
        Document doc = parser.build(new StringReader(responseXML));
        
        Element root = (doc.getRootElement());
        Iterator it = root.getDescendants();
        int tag=-1;
        while (it.hasNext()){
          Object o = (Object)it.next();
          if (o instanceof Element){
            Element e = (Element)o;
            //check status code on responseXML
            if(e.getName().equalsIgnoreCase("pageRank")){
              System.out.println(" : " + e.getValue());
              tag=0;
            }
          }
        }
        
        if (tag==-1) System.out.println(" : "+"Cannot be ranked by EasyAPI");
      }
    } catch (JDOMException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  /*
  * Generate XML request to server
  * @param url to be send
  * @return
  * */

  String generateXMLRequestForPageRank(String url){
    StringBuffer xml = new StringBuffer();
    String key="your_secret_key";
    xml.append("<easyapi_wrapper>");
    xml.append("<login>");
    xml.append("<apikey>"+ key +"</apikey>");
    xml.append("</login>");
    xml.append("<search>");
    xml.append("<service>googlepr</service>");
    xml.append("<criteria>");
    xml.append("<url>"+url+"</url>" );
    xml.append("</criteria>");
    xml.append("</search>");
    xml.append("</easyapi_wrapper>");
    
    return xml.toString();
  }

  /**
  * sends XML request to URLConnection and receives response
  * @param XMLrequest
  * @throws IOException
  */
  
  String requestPageRank(String XMLrequest){
    if(XMLrequest == null){
      System.out.println("LINK IS NULL, RETURNING NULL");
      return null;
    }
    
    //send request
    try {

      //System.out.println("Sending URL to check for page rank!");
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
      wr.write(XMLrequest);
      wr.flush();
  
      //get response from server
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line;
      String XMLresponse="";
      
      while ((line = rd.readLine()) != null){
        //System.out.println(line);
        XMLresponse=XMLresponse+line;
      }
      
      wr.close();
      rd.close();
      return XMLresponse;
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    return null;
  }             

  /**
  * @param args
  */

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Begin!");
    GoogleUtils util=new GoogleUtils();
    util.loadFile("t1.txt");
    //util.getConnection();
    util.callPageRank();
    System.out.println("End!");          
  }
}