geturl.lib.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_SAFE_UPLOAD, true); // PHP 5.5
  64. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // We want response
  65. if ($postorget == 'POST')
  66. {
  67. curl_setopt($ch, CURLOPT_POST, 1); // POST
  68. curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // Setting param x=a&y=z as POST fields
  69. }
  70. else if ($postorget == 'PUT')
  71. {
  72. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT'
  73. if (! is_array($param)) parse_str($param, $array_param);
  74. else
  75. {
  76. dol_syslog("parameter param must be a string", LOG_WARNING);
  77. $array_param=$param;
  78. }
  79. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param)); // Setting param x=a&y=z as PUT fields
  80. }
  81. else if ($postorget == 'PUTALREADYFORMATED')
  82. {
  83. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT'
  84. curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // param = content of post, like a xml string
  85. }
  86. else if ($postorget == 'HEAD')
  87. {
  88. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD'
  89. curl_setopt($ch, CURLOPT_NOBODY, true);
  90. }
  91. else if ($postorget == 'DELETE')
  92. {
  93. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); // POST
  94. }
  95. else
  96. {
  97. curl_setopt($ch, CURLOPT_POST, 0); // GET
  98. }
  99. //if USE_PROXY constant set at begin of this method.
  100. if ($USE_PROXY)
  101. {
  102. dol_syslog("getURLContent set proxy to ".$PROXY_HOST. ":" . $PROXY_PORT." - ".$PROXY_USER. ":" . $PROXY_PASS);
  103. //curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10
  104. curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);
  105. if ($PROXY_USER) curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER. ":" . $PROXY_PASS);
  106. }
  107. //getting response from server
  108. $response = curl_exec($ch);
  109. $request = curl_getinfo($ch, CURLINFO_HEADER_OUT); // Reading of request must be done after sending request
  110. dol_syslog("getURLContent request=".$request);
  111. dol_syslog("getURLContent response=".$response);
  112. $rep=array();
  113. if (curl_errno($ch))
  114. {
  115. // Ad keys to $rep
  116. $rep['content']=$response;
  117. // moving to display page to display curl errors
  118. $rep['curl_error_no']=curl_errno($ch);
  119. $rep['curl_error_msg']=curl_error($ch);
  120. dol_syslog("getURLContent response array is ".join(',',$rep));
  121. }
  122. else
  123. {
  124. $info = curl_getinfo($ch);
  125. // Ad keys to $rep
  126. $rep = $info;
  127. //$rep['header_size']=$info['header_size'];
  128. //$rep['http_code']=$info['http_code'];
  129. dol_syslog("getURLContent http_code=".$rep['http_code']);
  130. // Add more keys to $rep
  131. $rep['content']=$response;
  132. $rep['curl_error_no']='';
  133. $rep['curl_error_msg']='';
  134. //closing the curl
  135. curl_close($ch);
  136. }
  137. return $rep;
  138. }