Apa sih cURL
cURL adalah sebuah program/library untuk mengakses file melalui sebuah url. cURL sudah support banyak protokol seperti FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS dan FILE. Sebenarnya cURL adalah sebuah program command line di unix, tetapi sekarang sudah di support oleh php (menjadi extension php).
Keunggulan Curl
Kenapa cURL? bukankah kita juga menggunakan perintah bawaan php seperti file, readfile, file_get_content untuk mengakses url? iya kita bisa mendapat kan content dari sebuah url dengn perintah tersebut tetapi ada batasan di error handling, header, authentication, post form, upload file.
Library Curl untuk codeigniter
Sebelum memluai penggunaan curl sebagai library codeigniter, sebaiknya kita mengenali carai penggunaan cUrl terlebih dahulu. Sebuah request curl ternisi atas 4 steo utama yaitu:
- Initialize: tahap inisisalisasi bertujuan membentuk sebuah curl handle menggunakan curl_init()
- Set Options: Mengeset opsi2 yang dibutuh kan spt header, dan paramter lainnya
- Execute dan Fetch Result Mengirimkan request ke server
- Clear curl handler dengan perintah curl_close()
untuk mempermudah penggunaan maka fungsi curl tersebut dibungkus dan dapat didownload di link berikut
Download
Contoh penggunaan library CURL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Contoh extends Controller { function Contoh() { parent::Controller(); } function index() { $this->load->library('Curl'); $google_login_url = "https://www.google.com/accounts/ClientLogin"; $contact_url = 'http://www.google.com/m8/feeds/contacts/default/full?max-results=100'; $param = array( "accountType" => "GOOGLE", "Email" => 'xibnoe', "Passwd" => 'PASSWORD GW', "service" => "cp", "source" => "Webwoork/1.0" ); $out = $this->curl->post($google_login_url,$param); var_dump($out); } function google() { $this->load->library('Curl'); echo $this->curl->get('http://google.com') } } |


Thanks