Advertisement
Guest User

auspost drc

a guest
Sep 8th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. <?php
  2. function getPostRedirectURL($ch, $header)
  3. {
  4.  
  5. $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  6. // Request is not a redirect, so we don't need to follow it
  7. if(substr($responseCode, 0, 1) != 3) {
  8. return '';
  9. }
  10.  
  11. // Grab the location match/redirect from the headers
  12. if(!preg_match('#Location:(.*)\n#', $header, $matches)) {
  13. return '';
  14. }
  15. // Determine the new URL to redirect to.
  16. // A web server can respond with Location: /blah.php or Location: ?test
  17. // which means use the pieces from the previous location.
  18. $redirectUrl = parse_url(trim($matches[1]));
  19. $currentUrl = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
  20. if(empty($redirectUrl['scheme'])) {
  21. $redirectUrl['scheme'] = $currentUrl['scheme'];
  22. }
  23. if(empty($redirectUrl['host'])) {
  24. $redirectUrl['host'] = $currentUrl['host'];
  25. }
  26. if(empty($redirectUrl['port'])) {
  27. if(isset($currentUrl['port'])) {
  28. $redirectUrl['port'] = $currentUrl['port'];
  29. } else {
  30. $redirectUrl['port'] = '80';
  31. }
  32. }
  33. if(empty($redirectUrl['path'])) {
  34. $redirectUrl['path'] = $currentUrl['path'];
  35. }
  36.  
  37. $newUrl = $redirectUrl['scheme'].'://'.$redirectUrl['host'].$redirectUrl['path'];
  38. if($redirectUrl['query']) {
  39. $newUrl .= '?'.$redirectUrl['query'];
  40. }
  41. return $newUrl;
  42.  
  43. }
  44.  
  45. function PostToRemoteFileAndGetResponse($Path, $Vars="", $timeout=60, &$error = null)
  46. {
  47. if(function_exists("curl_exec")) { echo "using curl<br>";
  48. // Use CURL if it's available
  49. $ch = curl_init($Path);
  50.  
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  52. if($timeout > 0 && $timeout !== false) {
  53. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  54. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  55. }
  56.  
  57. // Setup the proxy settings if there are any
  58. /*if (GetConfig('HTTPProxyServer')) {
  59. curl_setopt($ch, CURLOPT_PROXY, GetConfig('HTTPProxyServer'));
  60. if (GetConfig('HTTPProxyPort')) {
  61. curl_setopt($ch, CURLOPT_PROXYPORT, GetConfig('HTTPProxyPort'));
  62. }
  63. }
  64.  
  65. if (GetConfig('HTTPSSLVerifyPeer') == 0) {
  66. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  67. }*/
  68.  
  69. // A blank encoding means accept all (defalte, gzip etc)
  70. if (defined('CURLOPT_ENCODING')) {
  71. curl_setopt($ch, CURLOPT_ENCODING, '');
  72. }
  73.  
  74. if($Vars != "") {
  75. curl_setopt($ch, CURLOPT_POST, 1);
  76. curl_setopt($ch, CURLOPT_POSTFIELDS, $Vars);
  77. }
  78.  
  79. if (!ISC_SAFEMODE && ini_get('open_basedir') == '') {
  80. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  81. $result = curl_exec($ch);
  82. } else {
  83. curl_setopt($ch, CURLOPT_HEADER, true);
  84.  
  85. $curRequest = 1;
  86. while($curRequest <= 10) {
  87. $result = curl_exec($ch);
  88.  
  89. // For any responses that include a 1xx Informational response at the
  90. // start, strip those off. An informational response is a response
  91. // consisting of only a status line and possibly headers. Terminated by CRLF.
  92. while(preg_match('#^HTTP/1\.1 1[0-9]{2}#', $result) && preg_match('#\r?\n\r?\n#', $result, $matches)) {
  93. $result = substr($result, strpos($result, $matches[0]) + strlen($matches[0]));
  94. $result = ltrim($result);
  95. }
  96.  
  97. list($header, $result) = preg_split('#\r?\n\r?\n#', $result, 2);
  98.  
  99. $newUrl = getPostRedirectURL($ch, $header);
  100. if($newUrl == '') {
  101. break;
  102. }
  103. curl_setopt($ch, CURLOPT_URL, $newUrl);
  104. $curRequest++;
  105. }
  106. }
  107.  
  108. if ($result === false) {
  109. // something failed... there's quite a few other curl error codes but these are the most common
  110. // using numbers here instead of constants due to changes in php versions and libcurl versions
  111. $curlError = curl_errno($ch);
  112. switch ($curlError) {
  113. case 1: //CURLE_UNSUPPORTED_PROTOCOL
  114. case 2: //CURLE_FAILED_INIT
  115. case 3: //CURLE_URL_MALFORMAT
  116. case 7: //CURLE_COULDNT_CONNECT
  117. case 27: //CURLE_OUT_OF_MEMORY
  118. case 41: //CURLE_FUNCTION_NOT_FOUND
  119. case 55: //CURLE_SEND_ERROR
  120. case 56: //CURLE_RECV_ERROR
  121. $error = ISC_REMOTEFILE_ERROR_SENDFAIL;
  122. break;
  123.  
  124. case 47: //CURLE_TOO_MANY_REDIRECTS
  125. $error = ISC_REMOTEFILE_ERROR_TOOMANYREDIRECTS;
  126. break;
  127.  
  128. case 22: //CURLE_HTTP_RETURNED_ERROR
  129. $error = ISC_REMOTEFILE_ERROR_HTTPERROR;
  130. break;
  131.  
  132. case 52: //CURLE_GOT_NOTHING
  133. $error = ISC_REMOTEFILE_ERROR_EMPTY;
  134. break;
  135.  
  136. case 67: //CURLE_LOGIN_DENIED
  137. $error = ISC_REMOTEFILE_ERROR_LOGINDENIED;
  138. break;
  139.  
  140. case 28: //CURLE_OPERATION_TIMEDOUT
  141. $error = ISC_REMOTEFILE_ERROR_TIMEOUT;
  142. break;
  143.  
  144. case 5: //CURLE_COULDNT_RESOLVE_PROXY:
  145. case 6: //CURLE_COULDNT_RESOLVE_HOST:
  146. $error = ISC_REMOTEFILE_ERROR_DNSFAIL;
  147. break;
  148.  
  149. default:
  150. $error = ISC_REMOTEFILE_ERROR_UNKNOWN;
  151. break;
  152. }
  153. }
  154.  
  155. return $result;
  156. }
  157. else { echo "using fsockopen<br>";
  158. // Use fsockopen instead
  159. $Path = @parse_url($Path);
  160. if(!isset($Path['host']) || $Path['host'] == '') {
  161. $error = ISC_REMOTEFILE_ERROR_NOHOST;
  162. return null;
  163. }
  164. if(!isset($Path['port'])) {
  165. $Path['port'] = 80;
  166. }
  167. if(!isset($Path['path'])) {
  168. $Path['path'] = '/';
  169. }
  170. if(isset($Path['query'])) {
  171. $Path['path'] .= "?".$Path['query'];
  172. }
  173.  
  174. if(isset($Path['scheme']) && strtolower($Path['scheme']) == 'https') {
  175. $socketHost = 'ssl://'.$Path['host'];
  176. $Path['port'] = 443;
  177. }
  178. else {
  179. $socketHost = $Path['host'];
  180. }
  181.  
  182. $fp = @fsockopen($Path['host'], $Path['port'], $errorNo, $error, 5);
  183. if(!$fp) {
  184. $error = ISC_REMOTEFILE_ERROR_SENDFAIL;
  185. return null;
  186. }
  187.  
  188. $headers = array();
  189.  
  190. // If we have one or more variables, perform a post request
  191. if($Vars != '') {
  192. $headers[] = "POST ".$Path['path']." HTTP/1.0";
  193. $headers[] = "Content-Length: ".strlen($Vars);
  194. $headers[] = "Content-Type: application/x-www-form-urlencoded";
  195. }
  196. // Otherwise, let's get.
  197. else {
  198. $headers[] = "GET ".$Path['path']." HTTP/1.0";
  199. }
  200. $headers[] = "Host: ".$Path['host'];
  201. $headers[] = "Connection: Close";
  202. $headers[] = ""; // Extra CRLF to indicate the start of the data transmission
  203.  
  204. if($Vars != '') {
  205. $headers[] = $Vars;
  206. }
  207.  
  208. if(!fwrite($fp, implode("\r\n", $headers))) {
  209. @fclose($fp);
  210. return false;
  211. }
  212.  
  213. if($timeout > 0 && $timeout !== false) {
  214. @stream_set_timeout($fp, $timeout);
  215. }
  216.  
  217. $result = '';
  218. $meta = stream_get_meta_data($fp);
  219. while(!feof($fp) && !$meta['timed_out']) {
  220. $result .= @fgets($fp, 12800);
  221. $meta = stream_get_meta_data($fp);
  222. }
  223.  
  224. @fclose($fp);
  225.  
  226. if ($meta['timed_out']) {
  227. $error = ISC_REMOTEFILE_ERROR_TIMEOUT;
  228. return null;
  229. }
  230.  
  231. if (!$result) {
  232. $error = ISC_REMOTEFILE_ERROR_EMPTY;
  233. return null;
  234. }
  235.  
  236. // Strip off the headers. Content starts at a double CRLF.
  237. list($header, $result) = preg_split('#\r?\n\r?\n#', $result, 2);
  238. return $result;
  239. }
  240. }
  241.  
  242. $ausPostURL = 'http://drc.edeliver.com.au/ratecalc.asp?';
  243. $postVars = array(
  244. 'Height' => 70,
  245. 'Length' => 300,
  246. 'Width' => 400,
  247. 'Weight' => 3000,
  248. 'Quantity' => 1,
  249. 'Pickup_Postcode' => 2000,
  250. 'Destination_Postcode' => 3000,
  251. 'Country' => 'US',
  252. 'Service_Type' => 'eci_d'
  253. );
  254. //Service_Type can be: Sea, Air, Standard, eci_m, eci_d
  255. $postRequest = '';
  256. foreach($postVars as $k => $v) {
  257. $postRequest .= '&'.$k.'='.urlencode($v);
  258. }
  259. $postRequest = ltrim($postRequest, '&');
  260.  
  261. $result = PostToRemoteFileAndGetResponse($ausPostURL, $postRequest);
  262.  
  263. echo $result;
  264.  
  265.  
  266. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement