HTTP_Request2 POST参数被忽略了吗?

时间:2014-12-07 06:15:04

标签: php post http-request2

像curl这样的查询可以正常工作:

curl -XPOST -H "Content-Type: application/json" -d '{"query":"porges","start":0,"rows":10,"lang":"ENGLISH"}' http://localhost:8080/services/rest/index/z56508/search/field/search

我的情况我在那里得到11次点击。但是,当我尝试将其转换为HTTP_Request2时,调用将返回数据库中的所有匹配。

我看了Http_Request2 POST request not working来编写代码:

    require_once 'HTTP/Request2.php';
    $url = "http://localhost:8080/services/rest/index/z56508/search/field/search";
    $data = array("query"=>"porges","start"=>"0","rows"=>"10","lang"=>"ENGLISH");

    $r = new HTTP_Request2($url);
    $r->setHeader('Content-Type', 'application/json;charset=UTF-8');
    $r->setMethod(HTTP_Request2::METHOD_POST)
            ->addPostParameter($data);
    $response = $r->send();
    $body = $response->getBody();
    echo $body;

我做错了什么?似乎"query" => "porges"被忽略了,但为什么呢?

1 个答案:

答案 0 :(得分:2)

阅读友善手册:http://pear.php.net/manual/en/package.http.http-request2.request.php#package.http.http-request2.request.body

当您想根据addPostParameter()application/x-www-form-urlencoded内容类型规则生成POST请求正文时,应使用

multipart/form-data(提示:这不是您尝试发送的JSON) 。如果您有预建的请求正文,请使用setBody()

$request->setBody('{"query":"porges","start":0,"rows":10,"lang":"ENGLISH"}');