Advertisement
Guest User

curl post sample

a guest
Mar 21st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. <?php
  2.  
  3.     function curl($url, $method = 'GET', $data = [], $timeout = 300) {
  4.         $c = curl_init();
  5.         curl_setopt($c, CURLOPT_URL, $url);
  6.         curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  7.         curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  8.         curl_setopt($c, CURLOPT_FAILONERROR, true);
  9.         curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $timeout);
  10.         curl_setopt($c, CURLOPT_TIMEOUT, $timeout);
  11.  
  12.         curl_setopt($c, CURLOPT_HTTPHEADER, array(
  13.             'Accept: application/json',
  14.             'Content-Type: application/json'
  15.         ));
  16.  
  17.         if ($method == 'POST') {
  18.             curl_setopt($c, CURLOPT_POST, true);
  19.             curl_setopt($c, CURLOPT_POSTFIELDS, $data);
  20.         }
  21.  
  22.         $content = (string) curl_exec($c);
  23.  
  24.         $http_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  25.  
  26.         $curl_error = curl_error($c);
  27.  
  28.         curl_close($c);
  29.  
  30.         return $content;
  31.     }
  32.  
  33.     $url = 'http://localhost/destination';
  34.  
  35.     $data = [
  36.         'nik' => '123',
  37.         'user_id' => 'user',
  38.         'password' => 'xxxxxx',
  39.         'ip_user' => '127.0.0.1'
  40.     ];
  41.  
  42.     // Merubah menjadi json
  43.     $data = json_encode($data);
  44.  
  45.     $response = curl($url, 'POST', $data);
  46.  
  47.     var_dump($response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement