Posted By

frederichoule on 04/01/11


Tagged

curl php post


Versions (?)

Who likes this?

13 people have marked this snippet as a favorite

tunandin
frederichoule
kwanhon
liamchapman
tolka
boksiora
conradwarhol
o0110o
Priestd09
Internet-Uzver
Alson
ringo380
Mello


Perfect cURL Function


 / Published in: PHP
 

  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  

You need to login to post a comment.