在localhost上继续使用cURL获得400错误

时间:2016-04-12 18:28:09

标签: php json 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.

以下是我用来发送post变量并通过curl获取内容的函数。执行提取的文件和提取其内容的文件都在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;
    }

最终,请求应该向接收文件发送两个post变量(“url”和“format”),并期望返回一个json字符串。

1 个答案:

答案 0 :(得分:0)

这不是原始问题的答案,但事实证明我正在以错误的方式思考我的问题。

我想使用页面A作为页面B的代理。并且从页面A传递预期页面B的相同变量。

通过简单地包含页面B而不是将预期参数卷曲到它,可以实现相同的效果。所以答案就是

6 -4 1 0 0 ... 0
-4 6 -4 1 0 ... 0
 1 -4 6 -4 1 ...0
 ................
 0 ... 1 -4 6 -4 1
 0 ... ...1 -4 6-4
 0 .. ... 0 1 -4 6