geturl.lib.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /* Copyright (C) 2008-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/lib/geturl.lib.php
  20. * \brief This file contains functions dedicated to get URL.
  21. */
  22. /**
  23. * Function get content from an URL (use proxy if proxy defined)
  24. *
  25. * @param string $url URL to call.
  26. * @param string $postorget 'POST', 'GET', 'HEAD'
  27. * @param string $param Parameters of URL (x=value1&y=value2)
  28. * @param string $followlocation 1=Follow location, 0=Do not follow
  29. * @param array $addheaders Array of string to add into header. Example: ('Accept: application/xrds+xml', ....)
  30. * @return array Returns an associative array containing the response from the server array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)
  31. */
  32. function getURLContent($url,$postorget='GET',$param='',$followlocation=1,$addheaders=array())
  33. {
  34. //declaring of global variables
  35. global $conf, $langs;
  36. $USE_PROXY=empty($conf->global->MAIN_PROXY_USE)?0:$conf->global->MAIN_PROXY_USE;
  37. $PROXY_HOST=empty($conf->global->MAIN_PROXY_HOST)?0:$conf->global->MAIN_PROXY_HOST;
  38. $PROXY_PORT=empty($conf->global->MAIN_PROXY_PORT)?0:$conf->global->MAIN_PROXY_PORT;
  39. $PROXY_USER=empty($conf->global->MAIN_PROXY_USER)?0:$conf->global->MAIN_PROXY_USER;
  40. $PROXY_PASS=empty($conf->global->MAIN_PROXY_PASS)?0:$conf->global->MAIN_PROXY_PASS;
  41. dol_syslog("getURLContent postorget=".$postorget." URL=".$url." param=".$param);
  42. //setting the curl parameters.
  43. $ch = curl_init();
  44. /*print $API_Endpoint."-".$API_version."-".$PAYPAL_API_USER."-".$PAYPAL_API_PASSWORD."-".$PAYPAL_API_SIGNATURE."<br>";
  45. print $USE_PROXY."-".$gv_ApiErrorURL."<br>";
  46. print $nvpStr;
  47. exit;*/
  48. curl_setopt($ch, CURLOPT_URL, $url);
  49. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  50. curl_setopt($ch, CURLOPT_SSLVERSION, 3); // Force SSLv3
  51. curl_setopt($ch, CURLOPT_USERAGENT, 'Dolibarr geturl function');
  52. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, ($followlocation?true:false));
  53. if (count($addheaders)) curl_setopt($ch, CURLOPT_HTTPHEADER, $addheaders);
  54. curl_setopt($ch, CURLINFO_HEADER_OUT, true); // To be able to retrieve request header and log it
  55. //turning off the server and peer verification(TrustManager Concept).
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  58. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)?5:$conf->global->MAIN_USE_CONNECT_TIMEOUT);
  59. curl_setopt($ch, CURLOPT_TIMEOUT, empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT)?30:$conf->global->MAIN_USE_RESPONSE_TIMEOUT);
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // We want response
  61. if ($postorget == 'POST')
  62. {
  63. curl_setopt($ch, CURLOPT_POST, 1); // POST
  64. curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // Setting param x=a&y=z as POST fields
  65. }
  66. else if ($postorget == 'HEAD')
  67. {
  68. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD'
  69. curl_setopt($ch, CURLOPT_NOBODY, true);
  70. }
  71. else
  72. {
  73. curl_setopt($ch, CURLOPT_POST, 0); // GET
  74. }
  75. //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
  76. if ($USE_PROXY)
  77. {
  78. dol_syslog("getURLContent set proxy to ".$PROXY_HOST. ":" . $PROXY_PORT." - ".$PROXY_USER. ":" . $PROXY_PASS);
  79. //curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10
  80. curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);
  81. if ($PROXY_USER) curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER. ":" . $PROXY_PASS);
  82. }
  83. //getting response from server
  84. $response = curl_exec($ch);
  85. $status = curl_getinfo($ch, CURLINFO_HEADER_OUT); // Reading of request must be done after sending request
  86. dol_syslog("getURLContent request=".$status);
  87. dol_syslog("getURLContent response=".$response);
  88. $rep=array();
  89. $rep['content']=$response;
  90. $rep['curl_error_no']='';
  91. $rep['curl_error_msg']='';
  92. if (curl_errno($ch))
  93. {
  94. // moving to display page to display curl errors
  95. $rep['curl_error_no']=curl_errno($ch);
  96. $rep['curl_error_msg']=curl_error($ch);
  97. dol_syslog("getURLContent curl_error array is ".join(',',$rep));
  98. }
  99. else
  100. {
  101. $info = curl_getinfo($ch);
  102. $rep['header_size']=$info['header_size'];
  103. //closing the curl
  104. curl_close($ch);
  105. }
  106. return $rep;
  107. }