Perfect cURL Function


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function xcurl($url,$ref=null,$post=array(),$ua="Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre",$print=false) {
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  4. if(!empty($ref)) {
  5. curl_setopt($ch, CURLOPT_REFERER, $ref);
  6. }
  7. curl_setopt($ch, CURLOPT_URL, $url);
  8. curl_setopt($ch, CURLOPT_HEADER, 0);
  9. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  11. if(!empty($ua)) {
  12. curl_setopt($ch, CURLOPT_USERAGENT, $ua);
  13. }
  14. if(count($post) > 0){
  15. curl_setopt($ch, CURLOPT_POST, 1);
  16. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  17. }
  18. $output = curl_exec($ch);
  19. curl_close($ch);
  20. if($print) {
  21. print($output);
  22. } else {
  23. return $output;
  24. }
  25. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.