×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Matt Parry
Added: Mar 22, 2017 3:40 PM
Views: 12
Tags: no tags
  1. function createJsonCurlRequest($type, $url, $params)
  2.     {
  3.         $postData = '';
  4.         if (!empty($params)) {
  5.             $postData = json_encode($params);
  6.         }
  7.         $ch2 = curl_init();
  8.         curl_setopt($ch2, CURLOPT_FAILONERROR, false);  // IMPORTANT - NEED TO ALLOW RESPONSE TO BE RETURNED
  9.         curl_setopt($ch2, CURLOPT_POST, $type == 'POST');
  10.         curl_setopt($ch2, CURLOPT_URL, $url);
  11.         curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
  12.         curl_setopt($ch2, CURLOPT_VERBOSE, 1);
  13.         curl_setopt($ch2, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  14.         curl_setopt($ch2, CURLOPT_POSTFIELDS, $postData);
  15.         curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
  16.         curl_setopt($ch2, CURLOPT_HEADER, false);
  17.         curl_setopt($ch2, CURLINFO_HEADER_OUT, true);
  18.         curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
  19.                 'Content-Type: application/json',
  20.                 'Content-Length: ' . strlen($postData))
  21.         );
  22.         return $ch2;
  23.     }
Comments disabled