$url = "www.domain.com"; $ch = curl_init($url);
$token = "generated token code"; curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', // for define content type that is json 'bearer: '.$token, // send token in header request 'Content-length: 100' // content length for example 100 characters (can add by strlen($fields)) ) );
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$fields = array( "username" => "user1", "password" => "passuser1", "gender" => 1 ); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close($ch);
class class_name_for_call_cURL { protected function getUrl() { return "www.domain.com"; } public function call_cURL() { $token = "generated token code"; $fields = array( "username" => "user1", "password" => "passuser1", "gender" => 1 ); $url = $this->getUrl(); $output = $this->_execute($fields, $url, $token); // if you want to get json data // $output = json_decode($output); if ($output == "OK") { return true; } else { return false; } } private function _execute($postData, $url, $token) { // for sending data as json type $fields = json_encode($postData); $ch = curl_init($url); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', // if the content type is json 'bearer: '.$token // if you need token in header ) ); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); $result = curl_exec($ch); curl_close($ch); return $result; } }
$class = new class_name_for_call_cURL(); var_dump($class->call_cURL()); // output is true/false