0

Im trying to send a POST request to an external website, from what iv read so far its not possible due to same origin policy. But i'v also read a proxy can bypass this.

Is this possible AT ALL if I don't have access to the external website? I can't seem to clarify this.

I just want to send an AJAX POST and get the response like when i use Chrome's Advanced REST Client.

2 Answers 2

1

Set your header with

Access-Control-Allow-Origin: http://foo.example
2
  • I don't have access to the external server
    – nicwhitts
    Dec 11, 2014 at 19:53
  • @nicwhitts then you're SOL.
    – idbehold
    Dec 11, 2014 at 22:02
0

To any other noobies in the problem

Slightly modified this: https://github.com/eslachance/php-transparent-proxy

Now accepts a posting url:

<?php
if(!function_exists('apache_request_headers')) {
// Function is from: http://www.electrictoolbox.com/php-get-headers-sent-from-browser/
    function apache_request_headers() {
        $headers = array();
        foreach($_SERVER as $key => $value) {
            if(substr($key, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
            }
        }
        return $headers;
    }
}
// Figure out requester's IP to shipt it to X-Forwarded-For
$ip = '';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    //echo "HTTP_CLIENT_IP: ".$ip;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    //echo "HTTP_X_FORWARDED_FOR: ".$ip;
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
    //echo "REMOTE_ADDR: ".$ip;
}

//
preg_match('@^(?:http://)?([^/]+)@i', $_SERVER['HTTP_REFERER'], $matches);
$host = $matches[1];
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
$domainName = "{$matches[0]}";
//
//writelog($_POST);

$method = $_SERVER['REQUEST_METHOD'];
$desturl;
    // parse the given URL
    if($method == "POST") {
        $desturl = $_POST['url'];
        unset($_POST['url']);
    }
    else if ($method == "GET") {
        $desturl = $_GET['url'];
        unset($_GET['url']);
    }


$response = proxy_request($desturl, ($method == "GET" ? $_GET : $_POST), $method);
$headerArray = explode("\r\n", $response[header]);
foreach($headerArray as $headerLine) {
    header($headerLine);
}
echo $response[content];


function proxy_request($url, $data, $method) {
// Based on post_request from http://www.jonasjohn.de/snippets/php/post-request.htm
    global $ip;
    // Convert the data array into URL Parameters like a=b&foo=bar etc.


    //$data = http_build_query($data);

    $data = json_encode($data, JSON_FORCE_OBJECT);
    writelog($data);
    $datalength = strlen($data);





    $url = parse_url($url);
    //echo $url;
    if ($url['scheme'] != 'http') { 
        die('Error: Only HTTP request are supported !');
    }

    // extract host and path:
    $host = $url['host'];
    $path = $url['path'];

    // open a socket connection on port 80 - timeout: 30 sec
    $fp = fsockopen($host, 80, $errno, $errstr, 30);

    if ($fp){

        $out="";
        if($method == "POST") {
            $out ="POST $path HTTP/1.1\r\n";
        } else {
            $out ="GET $path?$data HTTP/1.1\r\n";
        }

        //Todo Get headers and forward them...

        $requestHeaders = apache_request_headers();

        while ((list($header, $value) = each($requestHeaders))) {
            /*if($header !== "Connection" && $header !== "Host" && $header !== "Content-Length" 
                && $header !== "Content-Type"
                && $header !== "Origin"
                && $header !== "Referer"
                && $header !== "X-Requested-With"
                && $header !== "Accept-Encoding"
                ) {
               // $out.= "$header: $value\r\n";
                writelog("$header: $value\r\n");
            }*/
            if($header == "Cookie" && $header == "User-Agent" ) {
                 $out.= "$header: $value\r\n";
                //writelog("$header: $value\r\n");
            }
        }
        $out.= "Host: $host\r\n";
        $out.= "Content-Type: application/json; charset=UTF-8\r\n";
        $out.= "Content-Length: $datalength\r\n";
        $out.= "Connection: Close\r\n\r\n";
        $out.= $data;
        fwrite($fp, $out);


        $result = ''; 
        while(!feof($fp)) {
            // receive the results of the request
            $result .= fgets($fp, 128);

        }
    }
    else { 
        return array(
            'status' => 'err', 
            'error' => "$errstr ($errno)"
        );
    }

    // close the socket connection:
    fclose($fp);

    // split the result header from the content
    $result = explode("\r\n\r\n", $result, 2);

    $header = isset($result[0]) ? $result[0] : '';
    $content = isset($result[1]) ? $result[1] : '';


    // return as structured array:
    return array(
        'status' => 'ok',
        'header' => $header,
        'content' => $content
    );
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.