debuggable

 
Contact Us
 

How about a WebModel?

Posted on 28/5/06 by Felix Geisendörfer

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

The last 2 days I've been working on a Model that would allow me to query Google Analytics so I could have a cron job e-mailing me the statistics of the Sites I track overnight. Even so I would really like to release it I decided to go and ask Google first since I could imagine reasons for them to be angry about a technology like this.

Anyway, while I'm waiting for them to respond I think it'll be alright to release one of the results of this little project, called the WebModel. It's very simple in terms of beeing able to do HTTP(s) GET's and POST's, but I still think it could be interesting for people to look at:

class WebModel extends AppModel
{
    var $useTable = false;
   
    function httpPost($url, $vars = null, $headers = null, $cookie_file = null)
    {
        $vars = $this->__toUrlData($vars);

      $ch = curl_init();
      if (! $ch)
      {    
        return false;
      }
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
     
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);         

        curl_setopt($ch,CURLOPT_TIMEOUT,20);
      curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively     
       
        if (!empty($headers))
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                       
        if (!empty($cookie_file))
        {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
        }
           
      $response = curl_exec($ch);
      curl_close($ch);            
         
        return $response;        
    }
   
    function httpGet($url, $vars = null, $headers = null, $cookie_file = null)
    {
        if (!empty($vars))
            $url = $url.'?'.$this->__toUrlData($vars);


        $ch = curl_init();
       
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
             
        curl_setopt($ch, CURLOPT_URL, $url);
       
        if (!empty($headers))
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                       
        if (!empty($cookie_file))
        {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
        }
                       
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper('get'));
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
   
        $ret = curl_exec($ch);
        curl_close($ch);
       
        return $ret;  
    }  
   
    function __toUrlData($arrayData)
    {
        $postData = array();
       
        foreach ($arrayData as $key => $val)
        {
            array_push($postData, $key.'='.urlencode($val));
        }
       
        return join('&', $postData);
    }        
}

Whenever you need to work with REST Api's or want to play around with regex based Html parsing you can use this Class like this:

class GoogleAnalytics extends WebModel
{
    var $name = 'GoogleAnalytics';    
   
    function findAll($url, $vars = array())
    {
        $data = $this->$this->httpGet($url, $vars);

        // Some Regex Magic to follow
       
        return $parsedData;
    }
}

Let's hope Google responds and allows me to release the Google Analytics Model since it it really is a nice piece of reverse engineering and code ; ).

--Felix aka the_undefined

 

You can skip to the end and add a comment.

Guido said on May 28, 2006:

cool man!

I hope Google can buy you this code ;) $$$

Waiting for the release...

Felix Geisendörfer said on May 28, 2006:

Hey Guido: Unfortunatly I don't think they would buy it ... why should they? I mean if they wanted people to do stuff like this they would provide a REST Api like they do for Google Calendar. So I would already be happy if they would allow me to release it as Open Source (MIT License of course).

Because the last thing I want to get into is one of these: "Giant Coporation sues Student"-Stories. Especially not if it's Google ; ).

--Felix

How about a WebModel? said on Jun 12, 2006:

[...] How about a WebModel?: "The last 2 days I've been working on a Model that would allow me to query Google Analytics so I could have a cron job e-mailing me the statistics of the Sites I track overnight. Even so I would really like to release it I decided to go and ask Google first since I could imagine reasons for them to be angry about a technology like this. [...]

[...] I already talked about my CakePHP implementation of a Google Analytics API in some of my previous posts, and I also asked Google to clarify the legal status of such code. Almost a month has passed, and besides the initial we'll contact you about it again e-mail, I didn't hear anything from Google. [...]

twister  said on Mar 19, 2007:

Waiting for the response. Hope I will get the response very soon:-p

no prescription pay vicodin said on Oct 05, 2007:

no prescription vicodin buy prescription vicodin no needed

records vicodin prescription no said on Nov 03, 2007:

buy prescription no vicodin paying vicodin prescription no

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.