Advertisement
Guest User

kf_curl

a guest
Jan 21st, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1.     public function postJson($url, array $fields) {
  2.         $client = curl_init();
  3.  
  4.         $data      = json_encode($fields);
  5.         $headers   = $this->flattenHeaders();
  6.         $headers[] = 'Content-Length: ' . strlen($data);
  7.  
  8.         // Default parameters
  9.         curl_setopt_array($client, [
  10.             CURLOPT_TIMEOUT        => 30,
  11.             CURLOPT_RETURNTRANSFER => true,
  12.             CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,
  13.             CURLOPT_USERPWD        => $this->authHeader,
  14.             CURLOPT_URL            => $this->buildUrl($url),
  15.             CURLOPT_POST           => true,
  16.             CURLOPT_POSTFIELDS     => $data,
  17.             CURLOPT_HTTPHEADER     => $headers,
  18.         ]);
  19.  
  20.         $body = curl_exec($client);
  21.  
  22.         if (curl_errno($client)) {
  23.             throw new CurlException(curl_error($client), curl_errno($client));
  24.         }
  25.  
  26.         $result = CurlResponse::factory($body, $client);
  27.  
  28.         curl_close($client);
  29.  
  30.         return $result;
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement