Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. $url = 'env.host.com/api/endpoint';
  2.  
  3. $data = [
  4. 'json' => '{"clockNo":"3021","startTime":"2019-10-17 17:09:39","endTime":"2019-10-17 17:09:39","workCentreNumber":"118","so":"1702-01","minutes":0,"source":"Scanner","active":1,"operation_id":"613","operation_type":"queue"}'
  5. ];
  6.  
  7. $payload = http_build_query($data);
  8. //json=%7B%22clockNo%22%3A%223021%22%2C%22startTime%22%3A%222019-10-17+17%3A49%3A06%22%2C%22endTime%22%3A%222019-10-17+17%3A49%3A06%22%2C%22workCentreNumber%22%3A%22118%22%2C%22so%22%3A%221704-01%22%2C%22minutes%22%3A0%2C%22source%22%3A%22Scanner%22%2C%22active%22%3A1%2C%22operation_id%22%3A%22449%22%2C%22operation_type%22%3A%22queue%22%7D
  9. //  Too big??
  10.  
  11. $this->httpPost($url, $payload);
  12.  
  13. protected function httpPost($url, $data)
  14.     {
  15.         $ch = curl_init(); //cURL handle
  16.  
  17.         curl_setopt_array($ch, [
  18.             CURLOPT_HEADER => true,
  19.             CURLOPT_POST => true,
  20.             CURLOPT_POSTFIELDS => $payload,
  21.             CURLOPT_RETURNTRANSFER => 1,
  22.             CURLOPT_URL => $url,
  23.             CURLOPT_HTTPHEADER => array('Expect:')
  24.         ]);
  25.  
  26.         if ($result = curl_exec($ch)) {
  27.  
  28.             $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  29.             $response['header'] = substr($result, 0, $header_size);
  30.             $response['content'] = substr($result, $header_size);
  31.  
  32.             $response['seconds'] = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
  33.             $response['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  34.             $response['bytes'] = curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
  35.             $response['bytes'] .= ' B (' . number_format(($response['bytes'] / 1024 / 1024), 3) . ' MB)';
  36.  
  37.         } else {
  38.             die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
  39.         }
  40.  
  41.         curl_close($ch);
  42.  
  43.         return $response;
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement