Mengirim Mixed Format dengan PHP cURL

PHP cURL

Dalam pembahasan kali ini, saya akan mencoba memaparkan bagaimana teknik melakukan pengiriman data dengan POST method dari suatu server ke server lain menggunakan cURL pada PHP.

Sebagai contoh,  misalkan teman-teman punya data di localhost (PC local). Selanjutnya teman-teman ingin mengirim suatu data tersebut ke server hosting.kita tahu bahwa hampir semua admin hosting menutup port untuk koneksi database nya jika diakses secara remote.Lalu bagaimana kita bisa mengirimkan data antar 2 Komputer ?

Disinilah cURL dimanfaatkan, hanya saja secara default di beberapa paket program yang berisi Apache, MySQL dan PHP seperti misalnya AppServ, belum menyediakan modul CURL di dalam PHP nya. Dengan kata lain, sobat malasbaca harus menambahkan modul CURL ini sendiri.

Berikut langkah-langkah untuk menambahkan module cURL jika belum terinstall

  1. Buka file php.ini
  2. ctrl+f , dan cari kata extension=php_curl.dll, Jika ada tanda semicolon (titik koma) di depannya, maka hilangkanlah.
  3. download file cURL library libeay32.dll dan ssleay32.dll. teman-teman dapat mendownload file libeay32.dll di https://www.dll-files.com/libeay32.dll.html
  4. download file ssleay32.dll di https://www.dll-files.com/ssleay32.dll.html
  5. setelah itu letakkan pada direktori C:\Windows\System32. Kemudian restart komputer.

untuk mengecek apakah modul cURL sudah terinstal dengan baik atau belum di PHP Anda, silakan buat script berikut ini :

<?php phpinfo(); ?>

Kemudian jalankan script tersebut di browser. Jika dalam tampilan yang muncul di browser muncul informasi berikut ini

phpinfo

maka cURL sudah terinstal di PHP Anda.

Lanjut, ke pembahasan bagaimana kita bisa mengirim mixed format cURL pada PHP ?

Secara umum, jika kita menggunakan CURL untuk mengirim beberapa data (x, y dan z) dengan POST method ke server misal ke http://palak.in, perintahnya adalah sbb:

<?php
	$url = 'http://localheart/Tutorialku/curl_mixed/tangkap.php';
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_POST,1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	$result = curl_exec ($ch);
	curl_close ($ch);

berhubung kita akan POST data mixed format post data string dll dan post data file (binary), maka scriptnya menjadi :

<?php
	$url = 'http://localheart/Tutorialku/curl_mixed/tangkap.php';


	$ch = curl_init();
	if (function_exists('curl_file_create')) 
	{//jika ada fungsi curl_file_create maka gunakan
		$cFile = curl_file_create('file.txt');
	} 
	else 
	{//jika TIDAK ADA fungsi curl_file_create maka akali dengan tanda @ seperti pada bash (teman-teman linux geek pasti tau lah :v)
		$cFile = '@' . realpath('file.txt');
	}

	$post = array('name' => 'Nando','age'=>19,'file'=> $cFile);
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_POST,1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	$result = curl_exec ($ch);
	curl_close ($ch);
        print_r($result);//cetak rekursif output response server yang ditembak
        die();//breakpoint

curl_file_create parameternya adalah file yang mau diPOST. Sekedar saran, masukkan absolute path-nya contoh D:/test/file.txt

lalu,kita buat script sederhana untuk melihat data-data apa saja yang diPOST pada tangkap.php yang berisi :

<?php
	echo "<pre>";
	var_dump ($_FILES);//membedah semua variable files

	var_dump ($_POST); //membedah semua variable post
	die();
?>

Setelah itu jika berhasil outputnya akan seperti ini :

Output response dari server setelah diPOST

Sekian dulu pembahasan kali ini, . Jika kalian punya pertanyaan atau pendapat, silahkan isi di kolom komentar.

About Roby Firnando 4 Articles
a Computer Nerd , 0% Have Fun

15 Comments

  1. First off I want to say awesome blog! I had a quick question which
    I’d like to ask if you don’t mind. I was curious to
    know how you center yourself and clear your mind prior to writing.
    I’ve had a hard time clearing my thoughts in getting my ideas out.

    I do enjoy writing but it just seems like the first 10 to 15 minutes are
    generally lost simply just trying to figure out how to begin. Any ideas or
    tips? Thanks!

  2. You’ve made some decent points there. I looked on the net to
    find out more about the issue and found most individuals will
    go along with your views on this web site.

  3. Hi there, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of
    spam remarks? If so how do you prevent it, any plugin or anything you can recommend?

    I get so much lately it’s driving me mad so any support is very much
    appreciated.

  4. I have been exploring for a bit for any high quality articles or weblog posts on this sort
    of area . Exploring in Yahoo I finally stumbled upon this web site.
    Studying this info So i am glad to convey that I have
    an incredibly excellent uncanny feeling I came upon exactly what I needed.
    I so much indubitably will make sure to don?t disregard this site and provides
    it a look regularly.

  5. First off I want to say awesome blog! I had a quick question that
    I’d like to ask if you do not mind. I was interested to know how you center yourself
    and clear your thoughts prior to writing. I’ve had trouble clearing my thoughts in getting my thoughts out there.
    I truly do take pleasure in writing however it just seems like the
    first 10 to 15 minutes are usually wasted simply just trying to
    figure out how to begin. Any recommendations or hints?
    Many thanks!

  6. You really make it seem so easy with your presentation but I find
    this matter to be really something which I think I would never understand.

    It seems too complex and extremely broad for me.
    I am looking forward for your next post, I will try to get
    the hang of it!

  7. Do you have a spam issue on this site; I also am a blogger, and I was wanting
    to know your situation; many of us have developed some nice procedures and we
    are looking to exchange strategies with others, be sure to shoot me an email if interested.

Leave a Reply

Your email address will not be published.


*