[share] Catatan Code PHP Sederhana Untuk Web Service

Bismillah…

sekedar catatan gw ajah …😛

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url','form','string','text','security','html','debug','api'));
		
		// Load client library for test
		$this->load->spark('curl/1.2.0');
		$this->load->spark('restclient/2.0.0');
		$this->load->library(array('guzzle','http'));

		// Others
		$this->directory = 'api/';
		$this->name = 'bpjs';
	}

ini untuk library http dengan method GET:

		$sep = 'XXXXXXXXXXXXXXXXXXX';
		$hash = bpjs_hash_request(); //exit(dump($hash));

		$this->http->useCurl();
		$this->http->setMethod('GET');
		$this->http->addHeader('X-cons-id', $hash['con']);
		$this->http->addHeader('X-Timestamp', $hash['tim']);
		$this->http->addHeader('X-Signature', $hash['sig']);
		$this->http->execute('http://wwww.google.com/WsLokalRest/SEP/sep/'.$sep);
		echo "


<pre>"; echo ($this->http->error) ? $this->http->error : $this->http->result;
		$this->http->clear();

ini untuk library guzzle dengan method GET:

$url 	 = "http://www.google.com/WsLokalRest/SEP/";
		$hash 	 = bpjs_hash_request();
		
		$client  = new GuzzleHttp\Client([
            'base_uri' => $url,
            'timeout'  => 15.0,
            'headers' => [
                "User-Agent" => 'testing/1.0',
        	"Accept"     => 'application/json',
        	"X-cons-id" => $hash['con'],
        	"X-Timestamp" => $hash['tim'],
        	"X-Signature" => $hash['sig']
        	],
        ]);

		// $response = $client->get('sep/'.$sep, ['debug' => true]);
		$response = $client->request('GET', 'sep/'.$sep);
		// $response = $client->get('sep/'.$sep);

		// show data
		$contents = $response->getBody()->getContents();
		echo "


<pre>"; echo ($contents);

ini untul library curl dengan method GET:


	$hash = bpjs_hash_request();

		$this->curl->create('http://www.google.com/WsLokalRest/SEP/sep/'.$sep);

		$this->curl->http_method('GET');
		$this->curl->http_header('X-cons-id',$hash['con']);
		$this->curl->http_header('X-Timestamp',$hash['tim']);
		$this->curl->http_header('X-Signature',$hash['sig']);

		$result = $this->curl->get()->execute();
		echo "


<pre>"; echo $result;

ini untul library Guzzle dengan method POST:

// cara pertama
$res = $client->post('aplicaresws/rest/bed/create/'.$ppk_rsp, [
		    'json' => [
		        'kodekelas' => 'VVP',
		        'namaruang' => 'SUPER VIP',
		        'koderuang' => 'SUPER VIP',
		        'kapasitas' => 'KELAS UTAMA I',
		        'tersedia'  => 10
		    ]
		]);

// cara kedua 
		$res = $client->request('POST','aplicaresws/rest/bed/create/'.$ppk_rsp, [
			'json' => [
				'kodekelas' => 'VVP',
				'namaruang' => 'SUPER VIP',
				'koderuang' => 'SUPER VIP',
				'kapasitas' => 'KELAS UTAMA I',
				'tersedia'  => 10,
			]
		]);

// cara ketiga
		$res = $client->post('aplicaresws/rest/bed/create/'.$ppk_rsp, [
		    'headers' => ['Content-Type' => 'application/json'],
		    'body' => json_encode([
		        'kodekelas' => 'VVP',
		        'koderuang' => 'SUPER VIP',
		        'kapasitas' => 'KELAS UTAMA I',
		        'tersedia'  => 10
		    ]),
		    'future' => true
		]);

// source: http://stackoverflow.com/a/38060763

eh ternyata, guzzle 6 untuk PHP 5.5 sedangkan server gw pake 5.4 yaudah downgred lah pake guzzle 6…. cuman beda sikit ajah ini yang guzzel 5:

	
$client  = new GuzzleHttp\Client([
            'base_url' => $this->url_bed_bpjs,
            'timeout'  => 15.0,
            'defaults' => [
            'headers' => $this->header_bpjs
            ]
        ]);
// base_url itu guzzle 5, kalo base_uri itu guzzle 6
// headers di guzzle 5 dibuat array di defaults

Oya ini source guzzle yang akhirnya gw putuskan untuk dipake:

  1. Guzzle 5
  2. Guzzle 6
  3. Guzzle Terbaru
  4. Library Guzzle

Done, Have A Nice Day!

🙂

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: