0

I've been scouring around trying to understand curl and building headers but it seems no matter what I do I get the following error :

HTTP/1.1 400 Bad Request Date: Tue, 12 Apr 2016 18:15:33 GMT Server: Apache/2.2.29 (Unix) mod_wsgi/3.5 Python/2.7.10 PHP/5.6.10 mod_ssl/2.2.29 OpenSSL/0.9.8zh DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.22.0 Content-Length: 226 Connection: close Content-Type: text/html; charset=iso-8859-1

Bad Request

Your browser sent a request that this server could not understand.

The following is the function that I am using to send post variables and fetch the contents via curl. Both the file doing the fetching and the file whose contents are fetched are being hosted locally on MAMP

    $url = "./lib/otherpage.php";
    $data = array("url"=>$_POST["url"],"format"=>"json");
    function tryCurl($baseurl,$data)
    {
        $bodydata = array(json_encode($data));
        $bodystr = http_build_query($bodydata);

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL,$baseurl);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$bodystr);
        curl_setopt($ch, CURLOPT_PORT,8888);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array(
            "POST / HTTP/1.0",
            "Content-Type: application/x-www-form-urlencoded",
            "Content-Length: ".strlen($bodystr),
            "Host: localhost:8888/gt_dev/",
            "User-Agent: My-User-Agent 1.0",
            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Cache-Control: no-cache",
            "Accept-Language: en;q=0.7,en-us;q=0.3",
            "Connection: close",
        ));

        // Execute
        $result=curl_exec($ch);

        // Printing any errors..
        echo curl_error($ch);

        curl_close($ch);
        return $result;
    }

Ultimately, the request should send two post variables ("url" and "format") to the receiving file, and expect a json string in return.

6
  • have you tryied the full url path? Commented Apr 12, 2016 at 18:30
  • 2
    You're trying to do too much with the headers, and also, $bodydata = array(json_encode($data)); $bodystr = http_build_query($bodydata); is totally broken. Commented Apr 12, 2016 at 18:35
  • @AguV The full url path yields the same result. drew010 - Yeah, I don't know how it's supposed to work as I've tried everything. The code you're referring to I pulled from the following answer Commented Apr 12, 2016 at 18:58
  • what are you trying to do with $_POST["url"] ? make certain your $data is not null. Commented Apr 12, 2016 at 19:00
  • Are you trying to send json or form-urlencoded? Check your $bodydata variable! Commented Apr 12, 2016 at 19:03

1 Answer 1

0

This isn't so much an answer to the original issue but turns out I was thinking about my problem the wrong way.

I wanted to use page A as a proxy to page B. And to pass the same variables page B expected from Page A.

Turns out the same effect can be achieved by simply including page B, not curling the expected parameters to it. So the answer is just

include("./lib/otherpage.php");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.