geturl.lib.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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', 'PUT', 'PUTALREADYFORMATED', 'DELETE'
  27. * @param string $param Parameters of URL (x=value1&y=value2) or may be a formated content with PUTALREADYFORMATED
  28. * @param integer $followlocation 1=Follow location, 0=Do not follow
  29. * @param string[] $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_USERAGENT, 'Dolibarr geturl function');
  51. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, ($followlocation?true:false)); // We use @ here because this may return warning if safe mode is on or open_basedir is on
  52. if (count($addheaders)) curl_setopt($ch, CURLOPT_HTTPHEADER, $addheaders);
  53. curl_setopt($ch, CURLINFO_HEADER_OUT, true); // To be able to retrieve request header and log it
  54. // By default use tls decied by PHP.
  55. // You can force, if supported a version like TLSv1 or TLSv1.2
  56. if (! empty($conf->global->MAIN_CURL_SSLVERSION)) curl_setopt($ch, CURLOPT_SSLVERSION, $conf->global->MAIN_CURL_SSLVERSION);
  57. //curl_setopt($ch, CURLOPT_SSLVERSION, 6); for tls 1.2
  58. //turning off the server and peer verification(TrustManager Concept).
  59. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  60. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  61. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)?5:$conf->global->MAIN_USE_CONNECT_TIMEOUT);
  62. curl_setopt($ch, CURLOPT_TIMEOUT, empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT)?30:$conf->global->MAIN_USE_RESPONSE_TIMEOUT);
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // We want response
  64. if ($postorget == 'POST')
  65. {
  66. curl_setopt($ch, CURLOPT_POST, 1); // POST
  67. curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // Setting param x=a&y=z as POST fields
  68. }
  69. else if ($postorget == 'PUT')
  70. {
  71. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT'
  72. if (! is_array($param)) parse_str($param, $array_param);
  73. else
  74. {
  75. dol_syslog("parameter param must be a string", LOG_WARNING);
  76. $array_param=$param;
  77. }
  78. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param)); // Setting param x=a&y=z as PUT fields
  79. }
  80. else if ($postorget == 'PUTALREADYFORMATED')
  81. {
  82. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT'
  83. curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // param = content of post, like a xml string
  84. }
  85. else if ($postorget == 'HEAD')
  86. {
  87. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD'
  88. curl_setopt($ch, CURLOPT_NOBODY, true);
  89. }
  90. else if ($postorget == 'DELETE')
  91. {
  92. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); // POST
  93. }
  94. else
  95. {
  96. curl_setopt($ch, CURLOPT_POST, 0); // GET
  97. }
  98. //if USE_PROXY constant set at begin of this method.
  99. if ($USE_PROXY)
  100. {
  101. dol_syslog("getURLContent set proxy to ".$PROXY_HOST. ":" . $PROXY_PORT." - ".$PROXY_USER. ":" . $PROXY_PASS);
  102. //curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10
  103. curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);
  104. if ($PROXY_USER) curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER. ":" . $PROXY_PASS);
  105. }
  106. //getting response from server
  107. $response = curl_exec($ch);
  108. $request = curl_getinfo($ch, CURLINFO_HEADER_OUT); // Reading of request must be done after sending request
  109. dol_syslog("getURLContent request=".$request);
  110. dol_syslog("getURLContent response=".$response);
  111. $rep=array();
  112. if (curl_errno($ch))
  113. {
  114. // Ad keys to $rep
  115. $rep['content']=$response;
  116. // moving to display page to display curl errors
  117. $rep['curl_error_no']=curl_errno($ch);
  118. $rep['curl_error_msg']=curl_error($ch);
  119. dol_syslog("getURLContent response array is ".join(',',$rep));
  120. }
  121. else
  122. {
  123. $info = curl_getinfo($ch);
  124. // Ad keys to $rep
  125. $rep = $info;
  126. //$rep['header_size']=$info['header_size'];
  127. //$rep['http_code']=$info['http_code'];
  128. dol_syslog("getURLContent http_code=".$rep['http_code']);
  129. // Add more keys to $rep
  130. $rep['content']=$response;
  131. $rep['curl_error_no']='';
  132. $rep['curl_error_msg']='';
  133. //closing the curl
  134. curl_close($ch);
  135. }
  136. return $rep;
  137. }