bluecake Posted May 11, 2010 Share Posted May 11, 2010 Guys I am abit stuck here.. I have a class called curl() and a function called get_lat_long($postcode,$domain=null) that uses the class curl. But I don't know how to call the "curl class" and run the function get_lat_long() in the controller file in the cakePHP framework. So for example, how do I run that get_lat_long() in the posts_controller.php file in the controllers folder of the framework. Here is the code for the curl() class: /** * The curl class */ class curl { /** * COnstructor */ function __construct() { } function init_curl($ch,$url,$postfields=null,$follow=null,$cookie=null,$referer=null) { // Set url curl_setopt($ch, CURLOPT_URL, $url); // Enable Post if($postfields) { curl_setopt ($ch, CURLOPT_POST, 1); curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields); } if($follow) { curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1 ); } if($referer) { curl_setopt($ch, CURLOPT_REFERER, $referer); } //Enable SSL curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'); //Return results as string curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); return $ch; } // end function /* Grabs a page */ function get_page($options) { //Set options foreach($options AS $key=>$value) { $$key = $value; } $ch = curl_init(); $ch = $this->init_curl($ch,$url,$postfields,$follow,$cookie); $page = curl_exec($ch); curl_close($ch); return $page; } and here is the function that uses the curl class: /** * Returns a lat / long of a given postcode * */ function get_lat_long($postcode,$domain=null) { if(!$domain) { $domain = "co.uk"; } $url = "http://maps.google." . $domain . "/maps/geo?q=" . urlencode($postcode) . "&output=json&key=ABQIAAAAWjc0ZH2RENLxziofASg9ABQH987j_SlqISv1l93HS7ksPkvN9xRAXjKLSj-Yj2Xw7I6gP3RHQb4UQg"; $json = $this->curl->get_page(array("url"=>$url)); $store_data = json_decode(str_replace(""","\"",htmlentities($json))); //Take care of accents $lng = $store_data->Placemark[0]->Point->coordinates[0]; $lat = $store_data->Placemark[0]->Point->coordinates[1]; //Return if($lng && $lat) { return array('lat'=>$lat, 'lng'=>$lng ); } else { return false; } } Could some share some light on how to use the class and the function in cakephp! Thanks, Blue.. Quote Link to comment https://forums.phpfreaks.com/topic/201346-how-do-i-call-a-custom-class-in-the-cakephp-framework-need-some-light/ Share on other sites More sharing options...
Clientbag Posted May 12, 2010 Share Posted May 12, 2010 First locate folder vendors, then in that folder create another folder named curl and in that folder create the file curl.php with your code. then in your controller do: App::import('Vendor','curl',array('file'=>'curl/curl.php')); $curl = new Curl; // creates instance of that class Now when you want to access functions from that class just do $curl->get_page(array("url"=>$url)); // from your code Quote Link to comment https://forums.phpfreaks.com/topic/201346-how-do-i-call-a-custom-class-in-the-cakephp-framework-need-some-light/#findComment-1057351 Share on other sites More sharing options...
bluecake Posted May 14, 2010 Author Share Posted May 14, 2010 Thanks for your reply but I get the following error parse error, expecting `T_FUNCTION' in C:\wamp\www\versionOne\controllers\assessors_controller.php on line 7 Here is the code to line 7: $curl = new Curl; // creates instance of that class And this is my full code: <?php App::import('Vendor','curl',array('file'=>'curl/curl.php')); class AssessorsController extends AppController { var $name = 'Assessors'; $curl = new Curl; // creates instance of that class var $helpers = array('Html', 'Form'); function index() { $this->Assessor->recursive = 0; $this->set('assessors', $this->paginate()); } function view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid Assessor', true)); $this->redirect(array('action' => 'index')); } $this->set('assessor', $this->Assessor->read(null, $id)); } //ABQIAAAAKTQxwK9voUwewo30TXUphhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQPqIzmFOVR0ei9267lDn2Th4XtTw /** function add() { if (!empty($this->data)) { $this->Assessor->geocode($this->data['Assessor']['zip']); $this->Assessor->create(); if ($this->Assessor->save($this->data)) { $this->Session->setFlash(__('The Assessor has been saved', true)); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The Assessor could not be saved. Please, try again.', true)); } } } */ function add() { if (!empty($this->data)) { $this->Assessor->geocode($this->data['Assessor']['zip']); $latlong = $this->get_lat_long($this->data['Assessor']['zip']) print_r($latlong); } } function edit($id = null) { if (!$id && empty($this->data)) { $this->Session->setFlash(__('Invalid Assessor', true)); $this->redirect(array('action' => 'index')); } if (!empty($this->data)) { if ($this->Assessor->save($this->data)) { $this->Session->setFlash(__('The Assessor has been saved', true)); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The Assessor could not be saved. Please, try again.', true)); } } if (empty($this->data)) { $this->data = $this->Assessor->read(null, $id); } } function delete($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid id for Assessor', true)); $this->redirect(array('action' => 'index')); } if ($this->Assessor->del($id)) { $this->Session->setFlash(__('Assessor deleted', true)); $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__('The Assessor could not be deleted. Please, try again.', true)); $this->redirect(array('action' => 'index')); } function admin_index() { $this->Assessor->recursive = 0; $this->set('assessors', $this->paginate()); } function admin_view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid Assessor', true)); $this->redirect(array('action' => 'index')); } $this->set('assessor', $this->Assessor->read(null, $id)); } function admin_add() { if (!empty($this->data)) { $this->Assessor->create(); if ($this->Assessor->save($this->data)) { $this->Session->setFlash(__('The Assessor has been saved', true)); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The Assessor could not be saved. Please, try again.', true)); } } } function admin_edit($id = null) { if (!$id && empty($this->data)) { $this->Session->setFlash(__('Invalid Assessor', true)); $this->redirect(array('action' => 'index')); } if (!empty($this->data)) { if ($this->Assessor->save($this->data)) { $this->Session->setFlash(__('The Assessor has been saved', true)); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The Assessor could not be saved. Please, try again.', true)); } } if (empty($this->data)) { $this->data = $this->Assessor->read(null, $id); } } function admin_delete($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid id for Assessor', true)); $this->redirect(array('action' => 'index')); } if ($this->Assessor->del($id)) { $this->Session->setFlash(__('Assessor deleted', true)); $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__('The Assessor could not be deleted. Please, try again.', true)); $this->redirect(array('action' => 'index')); } #Private Functions /** * Returns a lat / long of a given postcode * */ function get_lat_long($postcode,$domain=null) { if(!$domain) { $domain = "co.uk"; } $url = "http://maps.google." . $domain . "/maps/geo?q=" . urlencode($postcode) . "&output=json&key=ABQIAAAAKTQxwK9voUwewo30TXUphhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQPqIzmFOVR0ei9267lDn2Th4XtTw"; // $json = $this->curl->get_page(array("url"=>$url)); $curl->get_page(array("url"=>$url)); // from your code $store_data = json_decode(str_replace(""","\"",htmlentities($json))); //Take care of accents $lng = $store_data->Placemark[0]->Point->coordinates[0]; $lat = $store_data->Placemark[0]->Point->coordinates[1]; //Return if($lng && $lat) { return array('lat'=>$lat, 'lng'=>$lng ); } else { return false; } } } ?> Do let me know if I have done anything silly! Thanks, Blue... Quote Link to comment https://forums.phpfreaks.com/topic/201346-how-do-i-call-a-custom-class-in-the-cakephp-framework-need-some-light/#findComment-1058141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.