PHP cURL GET & POST Posted on 2014-11-23 | In PHP | 12345678910111213141516171819202122232425262728293031323334<?phpfunction curlCrawler($url, $data=array(), $method='GET', $http_header = "application/x-www-form-urlencoded; charset=utf-8") { $fields_string = ''; foreach ($data as $key => $value) { $fields_string .= $key. '=' . $value . '&'; } $curl_obj = curl_init(); curl_setopt($curl_obj, CURLOPT_HEADER, 0); if (strtoupper($method) === 'POST') { curl_setopt($curl_obj, CURLOPT_URL, $url); curl_setopt($curl_obj, CURLOPT_POST, 1); curl_setopt($curl_obj, CURLOPT_POSTFIELDS, $fields_string); } elseif (strtoupper($method) === 'GET') { curl_setopt($curl_obj, CURLOPT_URL, $url . '?'. $fields_string); } curl_setopt($curl_obj, CURLOPT_HTTPHEADER, array($http_header, "Content-Length: " . strlen($fields_string))); curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_obj, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($curl_obj, CURLOPT_TIMEOUT, 20); $rtn = curl_exec($curl_obj); if (!curl_errno($curl_obj)) { $response = $rtn; } else { $response = 'Curl error: ' . curl_error($curl_obj); } curl_close($curl_obj); return $response;}