Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function getPostRedirectURL($ch, $header)
- {
- $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- // Request is not a redirect, so we don't need to follow it
- if(substr($responseCode, 0, 1) != 3) {
- return '';
- }
- // Grab the location match/redirect from the headers
- if(!preg_match('#Location:(.*)\n#', $header, $matches)) {
- return '';
- }
- // Determine the new URL to redirect to.
- // A web server can respond with Location: /blah.php or Location: ?test
- // which means use the pieces from the previous location.
- $redirectUrl = parse_url(trim($matches[1]));
- $currentUrl = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
- if(empty($redirectUrl['scheme'])) {
- $redirectUrl['scheme'] = $currentUrl['scheme'];
- }
- if(empty($redirectUrl['host'])) {
- $redirectUrl['host'] = $currentUrl['host'];
- }
- if(empty($redirectUrl['port'])) {
- if(isset($currentUrl['port'])) {
- $redirectUrl['port'] = $currentUrl['port'];
- } else {
- $redirectUrl['port'] = '80';
- }
- }
- if(empty($redirectUrl['path'])) {
- $redirectUrl['path'] = $currentUrl['path'];
- }
- $newUrl = $redirectUrl['scheme'].'://'.$redirectUrl['host'].$redirectUrl['path'];
- if($redirectUrl['query']) {
- $newUrl .= '?'.$redirectUrl['query'];
- }
- return $newUrl;
- }
- function PostToRemoteFileAndGetResponse($Path, $Vars="", $timeout=60, &$error = null)
- {
- if(function_exists("curl_exec")) { echo "using curl<br>";
- // Use CURL if it's available
- $ch = curl_init($Path);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- if($timeout > 0 && $timeout !== false) {
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- }
- // Setup the proxy settings if there are any
- /*if (GetConfig('HTTPProxyServer')) {
- curl_setopt($ch, CURLOPT_PROXY, GetConfig('HTTPProxyServer'));
- if (GetConfig('HTTPProxyPort')) {
- curl_setopt($ch, CURLOPT_PROXYPORT, GetConfig('HTTPProxyPort'));
- }
- }
- if (GetConfig('HTTPSSLVerifyPeer') == 0) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- }*/
- // A blank encoding means accept all (defalte, gzip etc)
- if (defined('CURLOPT_ENCODING')) {
- curl_setopt($ch, CURLOPT_ENCODING, '');
- }
- if($Vars != "") {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $Vars);
- }
- if (!ISC_SAFEMODE && ini_get('open_basedir') == '') {
- @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- $result = curl_exec($ch);
- } else {
- curl_setopt($ch, CURLOPT_HEADER, true);
- $curRequest = 1;
- while($curRequest <= 10) {
- $result = curl_exec($ch);
- // For any responses that include a 1xx Informational response at the
- // start, strip those off. An informational response is a response
- // consisting of only a status line and possibly headers. Terminated by CRLF.
- while(preg_match('#^HTTP/1\.1 1[0-9]{2}#', $result) && preg_match('#\r?\n\r?\n#', $result, $matches)) {
- $result = substr($result, strpos($result, $matches[0]) + strlen($matches[0]));
- $result = ltrim($result);
- }
- list($header, $result) = preg_split('#\r?\n\r?\n#', $result, 2);
- $newUrl = getPostRedirectURL($ch, $header);
- if($newUrl == '') {
- break;
- }
- curl_setopt($ch, CURLOPT_URL, $newUrl);
- $curRequest++;
- }
- }
- if ($result === false) {
- // something failed... there's quite a few other curl error codes but these are the most common
- // using numbers here instead of constants due to changes in php versions and libcurl versions
- $curlError = curl_errno($ch);
- switch ($curlError) {
- case 1: //CURLE_UNSUPPORTED_PROTOCOL
- case 2: //CURLE_FAILED_INIT
- case 3: //CURLE_URL_MALFORMAT
- case 7: //CURLE_COULDNT_CONNECT
- case 27: //CURLE_OUT_OF_MEMORY
- case 41: //CURLE_FUNCTION_NOT_FOUND
- case 55: //CURLE_SEND_ERROR
- case 56: //CURLE_RECV_ERROR
- $error = ISC_REMOTEFILE_ERROR_SENDFAIL;
- break;
- case 47: //CURLE_TOO_MANY_REDIRECTS
- $error = ISC_REMOTEFILE_ERROR_TOOMANYREDIRECTS;
- break;
- case 22: //CURLE_HTTP_RETURNED_ERROR
- $error = ISC_REMOTEFILE_ERROR_HTTPERROR;
- break;
- case 52: //CURLE_GOT_NOTHING
- $error = ISC_REMOTEFILE_ERROR_EMPTY;
- break;
- case 67: //CURLE_LOGIN_DENIED
- $error = ISC_REMOTEFILE_ERROR_LOGINDENIED;
- break;
- case 28: //CURLE_OPERATION_TIMEDOUT
- $error = ISC_REMOTEFILE_ERROR_TIMEOUT;
- break;
- case 5: //CURLE_COULDNT_RESOLVE_PROXY:
- case 6: //CURLE_COULDNT_RESOLVE_HOST:
- $error = ISC_REMOTEFILE_ERROR_DNSFAIL;
- break;
- default:
- $error = ISC_REMOTEFILE_ERROR_UNKNOWN;
- break;
- }
- }
- return $result;
- }
- else { echo "using fsockopen<br>";
- // Use fsockopen instead
- $Path = @parse_url($Path);
- if(!isset($Path['host']) || $Path['host'] == '') {
- $error = ISC_REMOTEFILE_ERROR_NOHOST;
- return null;
- }
- if(!isset($Path['port'])) {
- $Path['port'] = 80;
- }
- if(!isset($Path['path'])) {
- $Path['path'] = '/';
- }
- if(isset($Path['query'])) {
- $Path['path'] .= "?".$Path['query'];
- }
- if(isset($Path['scheme']) && strtolower($Path['scheme']) == 'https') {
- $socketHost = 'ssl://'.$Path['host'];
- $Path['port'] = 443;
- }
- else {
- $socketHost = $Path['host'];
- }
- $fp = @fsockopen($Path['host'], $Path['port'], $errorNo, $error, 5);
- if(!$fp) {
- $error = ISC_REMOTEFILE_ERROR_SENDFAIL;
- return null;
- }
- $headers = array();
- // If we have one or more variables, perform a post request
- if($Vars != '') {
- $headers[] = "POST ".$Path['path']." HTTP/1.0";
- $headers[] = "Content-Length: ".strlen($Vars);
- $headers[] = "Content-Type: application/x-www-form-urlencoded";
- }
- // Otherwise, let's get.
- else {
- $headers[] = "GET ".$Path['path']." HTTP/1.0";
- }
- $headers[] = "Host: ".$Path['host'];
- $headers[] = "Connection: Close";
- $headers[] = ""; // Extra CRLF to indicate the start of the data transmission
- if($Vars != '') {
- $headers[] = $Vars;
- }
- if(!fwrite($fp, implode("\r\n", $headers))) {
- @fclose($fp);
- return false;
- }
- if($timeout > 0 && $timeout !== false) {
- @stream_set_timeout($fp, $timeout);
- }
- $result = '';
- $meta = stream_get_meta_data($fp);
- while(!feof($fp) && !$meta['timed_out']) {
- $result .= @fgets($fp, 12800);
- $meta = stream_get_meta_data($fp);
- }
- @fclose($fp);
- if ($meta['timed_out']) {
- $error = ISC_REMOTEFILE_ERROR_TIMEOUT;
- return null;
- }
- if (!$result) {
- $error = ISC_REMOTEFILE_ERROR_EMPTY;
- return null;
- }
- // Strip off the headers. Content starts at a double CRLF.
- list($header, $result) = preg_split('#\r?\n\r?\n#', $result, 2);
- return $result;
- }
- }
- $ausPostURL = 'http://drc.edeliver.com.au/ratecalc.asp?';
- $postVars = array(
- 'Height' => 70,
- 'Length' => 300,
- 'Width' => 400,
- 'Weight' => 3000,
- 'Quantity' => 1,
- 'Pickup_Postcode' => 2000,
- 'Destination_Postcode' => 3000,
- 'Country' => 'US',
- 'Service_Type' => 'eci_d'
- );
- //Service_Type can be: Sea, Air, Standard, eci_m, eci_d
- $postRequest = '';
- foreach($postVars as $k => $v) {
- $postRequest .= '&'.$k.'='.urlencode($v);
- }
- $postRequest = ltrim($postRequest, '&');
- $result = PostToRemoteFileAndGetResponse($ausPostURL, $postRequest);
- echo $result;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement