简体   繁体   English

在本地主机上使用cURL不断收到400错误

[英]Keep getting 400 error with cURL on localhost

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 : 我一直在尝试理解curl和构建标头,但是无论我做什么我似乎都遇到以下错误:

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. 以下是我用来发送post变量并通过curl获取内容的函数。 Both the file doing the fetching and the file whose contents are fetched are being hosted locally on MAMP 进行提取的文件和提取其内容的文件都被本地托管在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. 最终,请求应将两个post变量(“ url”和“ format”)发送到接收文件,并期望返回json字符串。

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. 我想使用A页作为B页的代理。并传递与A页相同的B页变量。

Turns out the same effect can be achieved by simply including page B, not curling the expected parameters to it. 事实证明,只需将页面B包含在内,而不用将期望的参数卷曲到页面B上,即可达到相同的效果。 So the answer is just 所以答案就是

include("./lib/otherpage.php");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM