PHP CURL模拟GET及POST函数代码

  1. <?php
  2. function vcurl($url, $post = '', $cookie = '', $cookiejar = '', $referer = ''){
  3. $tmpInfo = '';
  4. $cookiepath = getcwd().'./'.$cookiejar;
  5. $curl = curl_init();
  6. curl_setopt($curl, CURLOPT_URL, $url);
  7. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //开源代码phpfensi.com
  8. if($referer) {
  9. curl_setopt($curl, CURLOPT_REFERER, $referer);
  10. } else {
  11. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  12. }
  13. if($post) {
  14. curl_setopt($curl, CURLOPT_POST, 1);
  15. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  16. }
  17. if($cookie) {
  18. curl_setopt($curl, CURLOPT_COOKIE, $cookie);
  19. }
  20. if($cookiejar) {
  21. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiepath);
  22. curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiepath);
  23. }
  24. //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  25. curl_setopt($curl, CURLOPT_TIMEOUT, 100);
  26. curl_setopt($curl, CURLOPT_HEADER, 0);
  27. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  28. $tmpInfo = curl_exec($curl);
  29. if (curl_errno($curl)) {
  30. echo '<pre><b>错误:</b><br />'.curl_error($curl);
  31. }
  32. curl_close($curl);
  33. return $tmpInfo;
  34. }
  35. ?>