simple PHP curl function
below example function is used to send the form data or post data to remote server . i have mentioned detailed explanation on comments.
Code
Download Code
function curl($url, $method = 'get', $header = null, $postdata = null, $timeout = 60) { $s = curl_init(); // initialize curl handler curl_setopt($s,CURLOPT_URL, $url); //set option URL of the location if ($header) curl_setopt($s,CURLOPT_HTTPHEADER, $header); //set headers if presents curl_setopt($s,CURLOPT_TIMEOUT, $timeout); //time out of the curl handler curl_setopt($s,CURLOPT_CONNECTTIMEOUT, $timeout); //time out of the curl socket connection closing curl_setopt($s,CURLOPT_MAXREDIRS, 3); //set maximum URL redirections to 3 curl_setopt($s,CURLOPT_RETURNTRANSFER, true); // set option curl to return as string ,don't output directly curl_setopt($s,CURLOPT_FOLLOWLOCATION, 1); curl_setopt($s,CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($s,CURLOPT_COOKIEFILE, 'cookie.txt'); //set a cookie text file, make sure it is writable chmod 777 permission to cookie.txt if(strtolower($method) == 'post') { curl_setopt($s,CURLOPT_POST, true); //set curl option to post method curl_setopt($s,CURLOPT_POSTFIELDS, $postdata); //if post data present send them. } else if(strtolower($method) == 'delete') { curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'DELETE'); //file transfer time delete } else if(strtolower($method) == 'put') { curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($s,CURLOPT_POSTFIELDS, $postdata); //file transfer to post ,put method and set data } curl_setopt($s,CURLOPT_HEADER, 0); // curl send header curl_setopt($s,CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1'); //proxy as Mozilla browser curl_setopt($s, CURLOPT_SSL_VERIFYPEER, false); // don't need to SSL verify ,if present it need openSSL PHP extension $html = curl_exec($s); //run handler $status = curl_getinfo($s, CURLINFO_HTTP_CODE); // get the response status curl_close($s); //close handler return $html; //return output } |

