Jika Anda belum pernah mendengar cURL: cURL (dibaca: si URL) singkatan dari Client URL dan dikembangkan oleh Daniel Stenberg pada tahun 1998 sebagai alat bantu command line untuk transfer files dengan sintaks URL melalui bermacam-macam protokol (FTP, HTTP, HTTPS, SCP, SFTP, TELNET, LDAP, dsb). Sedangkan libcurl adalah library portable yang menyediakan interface (untuk berbagai bahasa pemrograman, seperti Perl, Python, PHP, dsb) terhadap fungsionalitas cURL. Contoh menggunakan cURL lewat CLI (Command Line Interface) untuk me-retrieve (mendapatkan file HTML) sebuah halaman web: $ curl -L google.com Parameter $ curl -d "author=gedex&email=&comment=test&comment_post_ID=5 \ http://gedex.web.id/wp-comments-post.php Perintah di atas akan
mempost sebuah komentar di halaman WordPress saya untuk postingan dengan ID = 5. Parameter Langkah awal menggunakan cURL dalam PHP adalah menginisialisasi sebuah session (atau disebut juga membuat cURL resource), yaitu dengan: <?php $ch = curl_init(); //buat resource cURL ?> Setelah sebuah resource cURL terbentuk, <?php $ch = curl_init(); //buat resourcce cURL //set opsi URL curl_setopt($ch, CURLOPT_URL, "http://gedex.web.id/"); ?> Setelah itu kita bisa mendapatkan halamannya dengan memanggil fungsi <?php $ch = curl_init(); //buat resourcce cURL //set opsi URL curl_setopt($ch, CURLOPT_URL, "http://gedex.web.id/"); //dapatkan halaman URL dan berikan (cetak) ke browser curl_exec($ch); ?> Untuk membebaskan pemakaian resource sistem, ada baiknya menutup session dari resource cURL yang telah dibuat dengan fungsi <?php $ch = curl_init(); //buat resourcce cURL //set opsi URL curl_setopt($ch, CURLOPT_URL, "http://gedex.web.id/"); //dapatkan halaman URL dan berikan (cetak) ke browser curl_exec($ch); //tutup resource cURL curl_close($ch); ?> Pada contoh di atas, output halamannya langsung dicetak setelah memanggil fungsi <?php $ch = curl_init(); //buat resourcce cURL //set opsi URL dan opsi RETURNTRANSFER curl_setopt($ch, CURLOPT_URL, "http://gedex.web.id/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //dapatkan halaman URL dan berikan ke variabel $output $output = curl_exec($ch); //tutup resource cURL curl_close($ch); //ganti kata 'gedex' dengan 'dini' $output = str_replace('gedex', 'dini', $output); //cetak output echo $output; ?> Pada contoh di atas, halaman web (file HTML) tidak langsung dicetak setelah pemanggilan <?php $ch = curl_init(); //buat resourcce cURL //set opsi URL dan opsi FOLLOWLOCATION curl_setopt($ch, CURLOPT_URL, "http://www.google.com"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //dapatkan halaman URL curl_exec($ch); //tutup resource cURL curl_close($ch); ?> Dua opsi yang berkaitan dengan opsi <form action="http://gedex.web.id/wp-comments-post.php" method="post" id="commentform"> <p><input type="text" name="author" id="author" value="" size="22" tabindex="1" /> <label for="author"><small>Name (required)</small></label></p> <p><input type="text" name="email" id="email" value="" size="22" tabindex="2" /> <label for="email"><small>Mail (will not be published) (required)</small></label></p> <p><input type="text" name="url" id="url" value="" size="22" tabindex="3" /> <label for="url"><small>Website</small></label></p> <p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p> <p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" /> <input type="hidden" name="comment_post_ID" value="5" /> </p> </form> Ada lima field (author, email, url, comment dan comment_post_ID) yang bisa kita post. Mari kita tulis menggunakan cURL dalam PHP. Oh iya, saya lupa menjelaskan.. Opsi untuk melakukan request dengan metode HTTP POST adalah $data = author=gedex&email=&url=http://gedex.web.id&comment=testing post data in string using cURL&comment_post_ID=5 curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); Atau bisa juga dengan menggunakan array: $data = array('author' => 'gedex', 'email' => '', 'url' => 'http://gedex.web.id', 'comment' => 'testing post data in array using cURL', 'comment_post_ID' => '5'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); Nah untuk mempost data (memberi komentar di halaman ini) menggunakan cURL, kodenya kurang lebih seperti ini: <?php $ch = curl_init(); //buat resourcce cURL //set opsi URL dan opsi FOLLOWLOCATION curl_setopt($ch, CURLOPT_URL, "http://gedex.web.id/wp-comments-post.php"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //set opsi post dan data post $data = array('author' => 'gedex', 'email' => '', 'url' => 'http://gedex.web.id', 'comment' => 'testing post data in array using cURL', 'comment_post_ID' => '148'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //post data curl_exec($ch); //tutup resource cURL curl_close($ch); ?> Opsi lainnya adalah <?php $ch = curl_init(); //buat resourcce cURL //set opsi URL dan opsi lainnya curl_setopt($ch, CURLOPT_URL, "http://www.useragent.org/"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_USERAGENT, 'gedex web spider/1.0'); //crawl URL curl_exec($ch); ?> Ada banyak opsi lainnya seperti
Selain fungsi <?php $ch = curl_init(); //buat resourcce cURL //set opsi URL dan opsi lainnya curl_setopt($ch, CURLOPT_URL, "http://www.gedex.web.id/"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FILETIME, true); $outout = curl_exec($ch); //cetak info echo <pre> print_r (curl_getinfo($ch)); echo </pre> ?> Informasi yang didapat adalah mengenai request terakhir yang terjadi, seperti jumlah waktu yang dibutuhkan, content-type, ukuran header dsb. Saya menset Array ( [url] => http://gedex.web.id/wp-login.php?redirect_to=%2Fwp2.5%2Fwp-admin%2F [content_type] => text/html; charset=UTF-8 [http_code] => 200 [header_size] => 922 [request_size] => 164 [filetime] => 1210067015 [ssl_verify_result] => 0 [redirect_count] => 1 [total_time] => 15.346 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 1886 [speed_download] => 122 [speed_upload] => 0 [download_content_length] => 1886 [upload_content_length] => 0 [starttransfer_time] => 2.119 [redirect_time] => 13.227 ) Setelah melihat beberapa dasar penggunaan fungsi cURL, mari kita buat skrip sederhana yang lebih berguna:
Banyak aplikasi web berbasis PHP yang memanfaatkan cURL, diantaranya adalah:
Nah cukup sekian penjelasan singkat mengenai cURL dan libcurl dalam PHP. Semoga berguna. Happy coding with cURL .Referensi:
Apa itu Curl_setopt?curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); Set Option pada cURL yang berfungsi untuk mengembalikan transfer menjadi bentuk String. Script cURL diatas bertujuan untuk menampilkan isi halaman dari URL https://sandbox.
Apa itu PHP cURL?Client URL atau umumnya disingkat menjadi cURL, adalah project open source yang berupa program yang berjalan pada command line (CMD), namun saat ini sudah tersedia library pada banyak bahasa pemrograman seperti PHP, Phyton, dan masih banyak lainya.
Apa itu Metode cURL?Curl adalah sebuah program dan library untuk mengirim dan mengambil data melalui URL.
|