How about a WebModel?
Posted by Felix Geisendörfer, on May 28, 2006 - in PHP & CakePHP » DataSources, Models & Behaviors
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
-
-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-
-
{
-
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)
-
{
-
$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);
-
-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-
-
{
-
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
-
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
-
}
-
-
curl_setopt($ch, CURLOPT_VERBOSE, 1);
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
-
-
$ret = curl_exec($ch);
-
curl_close($ch);
-
-
return $ret;
-
}
-
-
function __toUrlData($arrayData)
-
{
-
-
foreach ($arrayData as $key => $val)
-
{
-
}
-
-
}
-
}
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';
-
-
{
-
$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