1
0

simple-curl-call.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2019 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. /**
  14. * execute a curl call to the given $url
  15. * @param string $url The request url
  16. * @param bool $port
  17. * @return bool|mixed
  18. */
  19. static function curlCall($url,$port=false) {
  20. $ret = false;
  21. $ch = curl_init();
  22. curl_setopt($ch, CURLOPT_URL, $url);
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  24. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
  25. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  26. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  27. curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
  28. //curl_setopt($ch, CURLOPT_HEADER, true);
  29. if(!empty($port)) {
  30. curl_setopt($ch, CURLOPT_PORT, $port);
  31. }
  32. $do = curl_exec($ch);
  33. if(is_string($do) === true) {
  34. $ret = $do;
  35. }
  36. else {
  37. error_log(var_export(curl_error($ch),true));
  38. }
  39. curl_close($ch);
  40. return $ret;
  41. }